0

I am taking a picture from the gallery and want to save this into my SQLite database. I created a method insertData(int Image, String Name) in my SQ_Lite_DB which extends the SQLiteOpenHelper class.

insertData method

public boolean insertData(int Image, String Name){
    SQLiteDatabase database = getReadableDatabase();
    ContentValues values = new ContentValues();
    values.put("image",Image);
    values.put("name",Name);

   long id =  database.insert("orders",null,values);
    database.close();
    return id > 0;
}

I am calling this method in my onActivityResult method of sq-lite class

SQLite class

public class SQLite extends AppCompatActivity {

RecyclerView recyclerView;
Button insertdatabtn;
EditText edittext;


@Override
protected void onCreate(Bundle savedInstanceState) {

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

    insertdatabtn = findViewById(R.id.insertdatabtn);

    new SQ_Lite_DB(SQLite.this).ReadSqliteData(SQLite.this);

    ArrayList<Model> list = new ArrayList<>();
    Adpter adpter = new Adpter(list,SQLite.this);
    recyclerView = findViewById(R.id.recyclerview);

    recyclerView.setAdapter(adpter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


    insertdatabtn.setOnClickListener(v->{
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent,11);});
}

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    edittext = findViewById(R.id.edittext);

    SQ_Lite_DB db = new SQ_Lite_DB(SQLite.this);

    if (resultCode==RESULT_OK&& requestCode==11){

        try {InputStream inputStream = getContentResolver().openInputStream(data.getData());
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);

            byte[] img = baos.toByteArray();

           database.inserdata(/*what to put here, a int value needed but I can get uri or bitmap only*/,editText.getText.toString)
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            Log.d("tag",e.getMessage());
        }
    }
}
}
Sachin Burdak
  • 337
  • 1
  • 3
  • 13
  • You probably [store the byte array into SQLite database](https://stackoverflow.com/questions/4191136/how-to-store-and-retrieve-a-byte-array-image-data-to-and-from-a-sqlite-databas) – Zain Jun 19 '21 at 16:05

1 Answers1

1

Try with following code, you can add one more column in you table for imageUri and save imagepath.

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 100);
        }
    });


 //The onActivityResult method will invoke after select image from gallery
 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch (requestCode){
        case 100:
            if(resultCode == RESULT_OK){
                try {
                    final Uri imageUri = imageReturnedIntent.getData();
                   String  imageUri = imageUri.toString();
                } catch (FileNotFoundException e){
                    e.printStackTrace();
                }
            }
     }
  }
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7
  • Maybe you want to say that I should store the Uri of the image. ok, I can save it but how to retrieve and show image from uri. I mean is it possible to save and retrieve images using Uri? – Sachin Burdak Jun 20 '21 at 04:14
  • yes it is possible you can save imageUri and also can display image in view using that uri – Dharmender Manral Jun 20 '21 at 04:15
  • using this property imageView.setImageURI(imageUri) – Dharmender Manral Jun 20 '21 at 04:20
  • can you please answer this question so that I can taste your solution? https://stackoverflow.com/questions/68053602/java-lang-outofmemoryerror-memory-exhausted-while-reading-data-from-sqlite-an?noredirect=1#comment120282218_68053602 – Sachin Burdak Jun 20 '21 at 07:41
  • Thanks bro! you helped me a lot! If possible please upvote my question. – Sachin Burdak Jun 20 '21 at 11:15