Java.lang.outofmemoryerror While Converting Over 100 Strings Into To Byte Arrays
Solution 1:
What i would suggest you to drop your approach, this would just not work with large set of image and you are already putting to much work on your thread to handle. One should never store image like that in the sqllite. You should just convert your bitmap to a file having unique name or could be same (depends upon your use case) then you can just save this file inside the app directory and save the file path in database. Here is some code to help you.
FilepictureFile= getOutputMediaFile(getActivity(), MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
return;
}
Bitmapbitmap=BitmapFactory.decodeByteArray(data,0,data.length);
FileOutputStreamfos=newFileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
private File getOutputMediaFile(Context context, int m) {
FilemediaStorageDir= context.getFilesDir();
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("Fade", "failed to create directory");
returnnull;
}
}
// Create a media file nameStringtimeStamp=newSimpleDateFormat("yyyyMMdd_HHmmss")
.format(newDate());
File mediaFile=newFile(mediaStorageDir.getPath()+File.separator
+ "IMG_" + timeStamp + ".JPG");
return mediaFile;
}
Now you have the file and now you can just store the file path in the database and when its needed you can always get your file from the storage using glide. This would also make your database fast to queries.
This way you wont need any changes in gradle or anywhere else. Try this.
Post a Comment for "Java.lang.outofmemoryerror While Converting Over 100 Strings Into To Byte Arrays"