mercredi 25 novembre 2015

Android : Storing Image names in sqlite and Stored Images In Server how to Display in Imageview

In My Application on Detail Activity i have Image view... in that Image View i want to load images from server

In server i create a new Folder and i upload images with .jpg extension and in my sqlite image field i just add the image names with .jpg extension(text format)

What i Need is When i click the detail activity the image view want to display image name based on the selection

here is my code:

public class DetailActivity extends AppCompatActivity {

    private ShareActionProvider mShareActionProvider;
    SqlLiteDbHelper dbHelper;
    SQLiteDatabase sqLiteDatabase;
    Cursor cursor;
    TextView detailtext;

    ImageView imageView;

    android.support.v7.widget.ShareActionProvider share;
    android.support.v7.app.ActionBar actionBar;
    String SelectedText,imagename;
    public static int updatedSize;
    public static int updatedcolor;
   static Boolean flagColorChange;
    Context context ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_activity);




        actionBar = getSupportActionBar();


        detailtext = (TextView) findViewById(R.id.detail);
        imageView = (ImageView) findViewById(R.id.images);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // URL of this  image
String imageUrl = "http://ift.tt/1lhyCfq" 

        Intent intent = getIntent();
        final String selectedData = intent.getStringExtra("selectedItem");
        actionBar.setTitle(selectedData);

        dbHelper = new SqlLiteDbHelper(this);
        try {
            dbHelper.openDataBase();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        sqLiteDatabase = dbHelper.getReadableDatabase();
        cursor = dbHelper.getdetails(sqLiteDatabase, selectedData);

        if (cursor.moveToFirst()) {

            detailtext.setText(cursor.getString(0));



            Picasso.with(this).load(imageUrl).into(imageView );

        }
    }

dbquery

public class SqlLiteDbHelper  extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "ay.sqlite";
private static final String DB_PATH_SUFFIX = "/databases/";
public static final String ITEMNO="itemno";
public static final String TITLE="title";
public static final String SUBCATEGORY="subcategory";
public static final String DETAIL="detail";
public static final String IMAGES="images";
static Context ctx;

public SqlLiteDbHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    ctx = context;
}
public void CopyDataBaseFromAsset() throws IOException {

    InputStream myInput = ctx.getAssets().open(DATABASE_NAME);

    // Path to the just created empty db
    String outFileName = getDatabasePath();

    // if the path doesn't exist first, create it
    File f = new File(ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX);
    if (!f.exists())
        f.mkdir();

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);


    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);

    }
    // Close the streams

    myOutput.flush();
    myOutput.close();
    myInput.close();

}

private static String getDatabasePath() {

    return ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX + DATABASE_NAME;

}

public SQLiteDatabase openDataBase() throws SQLException {

    File dbFile = ctx.getDatabasePath(DATABASE_NAME);
    if (!dbFile.exists()) {
        try {
            CopyDataBaseFromAsset();

            System.out.println("Copying sucess from Assets folder");

        } catch (IOException e) {

            throw new RuntimeException("Error creating source database", e);

        }

    }
    return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public Cursor gettitles(SQLiteDatabase db)
{
    db = this.getReadableDatabase();
    Cursor cursor;
    cursor = db.query(true, "record", new String[]{TITLE,ITEMNO + " as _id"}, null, null, TITLE, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}
public Cursor getsubcategory(SQLiteDatabase db,String sub)
{
    db=this.getReadableDatabase();
    Cursor cursor;
    cursor = db.query("record",new String[]{SUBCATEGORY,ITEMNO + " as _id"},TITLE + "='" +sub+"'",null,null,null,null);
    //cursor = db.query("record",new String[]{SUBCATEGORY},TITLE + "=" +sub,null,null,null,null);
    return cursor;
}
public Cursor getdetails(SQLiteDatabase db,String img)
{
    db=this.getReadableDatabase();
    Cursor cursor;
    cursor=db.query("record",new String[]{DETAIL,IMAGES,ITEMNO + " as _id"},SUBCATEGORY + "='" +img+"'",null,null,null,null);
    return cursor;
}

layout.xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
    android:layout_height="match_parent"xmlns:ads="http://ift.tt/GEGVYd" android:background="@mipmap/bg">
    <ImageView
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:id="@+id/images"
        android:layout_centerHorizontal="true"

        />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/detail"
    android:text="Welcome"
android:layout_below=="@+id/images"
    android:textAppearance="?android:textAppearanceLarge"
    />

Aucun commentaire:

Enregistrer un commentaire