0

Programming newbie here.

I am making an app that stores the data of all the items in a store.

The app has an activity that shows the item names and quantity in stock in a listview. (This is done)

Clicking on a listview item takes you to another activity which shows the details of that particular item, which includes an image (i don't know how to do this).

I added a column into the database with BLOB type like this.

 @Override
    public void onCreate(SQLiteDatabase db) {
    String SQL_CREATE_ITEMS_TABLE =  "CREATE TABLE "  
    + ItemEntry.TABLE_NAME + " ("
    + ItemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
    + ItemEntry.COLUMN_ITEM_IMAGE + "BLOB, "
    + ItemEntry.COLUMN_ITEM_NAME + " TEXT NOT NULL, "
    + ItemEntry.COLUMN_ITEM_QUANTITY 
    + " INTEGER NOT NULL DEFAULT 0, "
    + ItemEntry.COLUMN_ITEM_COST_PRICE 
    + " INTEGER NOT NULL, "
    + ItemEntry.COLUMN_ITEM_SELLING_PRICE + " INTEGER NOT NULL);";  
    db.execSQL(SQL_CREATE_ITEMS_TABLE);
    } 

I have put an "Add image" button in the Details activity, when clicked, it should take the user to the gallery and allow to select one photo and add the photo to the database and show the photo in an imageview in the same activity.

Here is my code for onClickListener on the addImage button.

addImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(
                    Intent.ACTION_PICK,                    
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            in.setType("image/*");
            in.putExtra("crop", "true");
            in.putExtra("outputX", 100);
            in.putExtra("outputY", 100);
            in.putExtra("scale", true);
            in.putExtra("return-data", true);

            startActivityForResult(in, 1);
        }
    });

Here is the code onActivityResult method.

@Override
protected void onActivityResult(int requestCode, int resultCode, 
Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK && data != null) {

        Uri imageUri = data.getData();
        mImage.setImageURI(imageUri);

    }
}

In the above code, i don't know how to store the photo in the database for every item.

I checked other answers which uses get("data") method which is leading to a Nullpoint exception. Some answers use TEXT type to create the column for image and others use BLOB, so it is really confusing.

I am not concerned about slowing down the app or running out of memory, so please help me do this in the simplest way possible.

ShashankAC
  • 1,016
  • 11
  • 25
  • You need to convert it into base64 ad store that string in database, and call that field while setting up imagview – Ankit Tale Mar 21 '18 at 13:10
  • You could save the URI of the image in your table. – SripadRaj Mar 21 '18 at 13:15
  • I agree with @SripadRaj , You should restore the URI of the image , and when you set image in imageview, you can use image fromuri to get the image . – cowboi-peng Mar 21 '18 at 13:34
  • Thanks, i used a global string variable to store the image URI, but in my onActivityResult() method, i am not able to update the variable to hold the URI. – ShashankAC Mar 23 '18 at 09:11

1 Answers1

0
  • You should use Uri of the image in your database table instead of blob.
  • First, you can convert the picture you got from the gallery or the camera into uri.
  • Save to the database.
  • When retrieving from the database, you will also import the data as uri and assign it to imageView.

Examine the following sample code blocks.

Convert

cv.put("questPhoto",uri.toString());

Retrieving

strPhoto=cursor.getString(cursor.getColumnIndexOrThrow("questPhoto"));
                uriph= Uri.parse(strPhoto);
                questions.setImgsoru(uriph);

Define class and adapter in your list objects.Like this,

Item.class

public class Item {

String arkadi;
Uri arkresmi;
Integer arkId;

public Item(){

}

public String getArkadi() {
    return arkadi;
}

public void setArkadi(String arkadi) {
    this.arkadi = arkadi;
}

public Uri getArkresmi() {
    return arkresmi;
}

public void setArkresmi(Uri arkresmi) {
    this.arkresmi = arkresmi;
}

public Integer getArkId() {
    return arkId;
}

public void setArkId(Integer arkId) {
    this.arkId = arkId;
}

}

Adapter.class

public class Adapter extends ArrayAdapter<Item> {


Context context;
ArrayList<Item> listarray=new ArrayList<>();
private int resource;

public Adapter(Context context, int resource, ArrayList<Item> listarray) {
    super(context, resource, listarray);
    this.context=context;
    this.resource=resource;
    this.listarray=listarray;
}
class ViewTutucuArks{
    CircleImageView crcImgpp;
    TextView txtarkisim;

    ViewTutucuArks(View v){
        crcImgpp= (CircleImageView) v.findViewById(R.id.crcimgarkresim);
        txtarkisim= (TextView) v.findViewById(R.id.txtarkismi);
    }
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view8=convertView;
    ViewTutucuArks viewTutucuArks;

    if(view8==null){
        LayoutInflater layoutInflater= (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view8=layoutInflater.inflate(R.layout.custom_list,parent,false);
        viewTutucuArks=new ViewTutucuArks(view8);
        view8.setTag(viewTutucuArks);
    }
    else{
        viewTutucuArks= (ViewTutucuArks) view8.getTag();
    }
    Item itemark=listarray.get(position);
    viewTutucuArks.crcImgpp.setImageURI(itemark.getArkresmi());
    viewTutucuArks.txtarkisim.setText(itemark.getArkadi());

    return view8;
}

}

yourActivity

private void getAndShowQuestionsOfTEOGTR() {
    Cursor cursor=mSQLiteDb.rawQuery("...",null);

    if (cursor!=null){
        if(cursor.moveToFirst()){
            do {
                ItemQuestions questions=new ItemQuestions();

       questions.setStrsoruid(cursor.getString(cursor.getColumnIndexOrThrow("_id")));
                                strqp=cursor.getString(cursor.getColumnIndexOrThrow("questPhoto"));
                uriqp= Uri.parse(strqp);
                questions.setImgsoru(uriqp);
                //there is more

                questArray.add(questions);

            }while (cursor.moveToNext());
        }cursor.close();
    }mSQLiteDb.close();
    yourAdapter=new yourAdapter(getApplicationContext(),R.layout.yourCustomList,questArray);
    yourList.setAdapter(adapterQuestions);
}

Finally, using the ListClickHandler, you can display the info and image of that object.

  • Hi, thanks, actually i am using a CursorAdapter, i am having trouble using the onActivityResult method, this method is called only when the activity ends? that means i will not be able to get the Image URI during runtime and store it in the database ? – ShashankAC Mar 23 '18 at 07:45
  • At the end of the OnActivityResult, you can save the image(uri) to the database with a method you have prepared. –  Mar 23 '18 at 10:31
  • Thanks, i was able to store the URIs of the images in the database, but when loading the image back from the database i am getting a SecurityException. – ShashankAC Mar 27 '18 at 06:17
  • Do you know how to get the byteArray of the image inside the onActivityResult method ? – ShashankAC Mar 27 '18 at 09:07