Reputation: 1856
I try to get images from the Gallery, but when I try to load a large image I'll get the following error after some time:
10-30 13:36:55.180 16246-16246/ua.khuta.freeturnforadmin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: ua.khuta.freeturnforadmin, PID: 16246
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
at java.lang.StringBuilder.append(StringBuilder.java:216)
at ua.khuta.freeturnforadmin.activities.NewsDetailActivity.onActivityResult(NewsDetailActivity.java:265)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
yourSelectedImage = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String image = Base64.encodeToString(byteArray, Base64.DEFAULT);
imagesToShow.add(new News.Image("", image + "base64"));
changedImages.add(new News.Image("fid_new", image));
adapter.notifyDataSetChanged();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
I have read this tutorial on d.android.com but it didn't help me.
EDITED: I have added your method and try next code, but after that i have got another exception
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
yourSelectedImage = decodeImage(selectedImage.getPath());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String image = Base64.encodeToString(byteArray, Base64.DEFAULT);
imagesToShow.add(new News.Image("", image + "base64"));
changedImages.add(new News.Image("fid_new", image));
adapter.notifyDataSetChanged();
}
}
}
and now I got:
Process: ua.khuta.freeturnforadmin, PID: 24895
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://media/external/images/media/33642 flg=0x1 }} to activity {ua.khuta.freeturnforadmin/ua.khuta.freeturnforadmin.activities.NewsDetailActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ua.khuta.freeturnforadmin.activities.NewsDetailActivity.onActivityResult(NewsDetailActivity.java:257)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Null pointer at line:
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
Upvotes: 0
Views: 528
Reputation: 18933
When you'r picking up your image from Gallery then use this function.
private void decodeImage(final String path) {
int targetW = iv.getWidth();
int targetH = iv.getHeight();
final BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
imageView.setImageBitmap(bitmap);
}
Now when you getting imagepath after that use this function like,
decodeImage(yourimagePath);
EDIT:
Your getting image from Gallery then use this code in onActivityResult() method.
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage, filePath,
null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
yourSelectedImage = c.getString(columnIndex);
c.close();
if (yourSelectedImage!= null) {
Log.v("Image", yourSelectedImage);
decodeImage(yourSelectedImage);
}
Upvotes: 1
Reputation: 2178
You can set largeHeap="true" in your manifest. Other than this there are various other technique to handle bitmaps which you can find here. There is a library named Picasso that can help you a lot. just don't cache the the image by skipMemoryCache(). I think this will help you a lot :)
Upvotes: 0