0

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)

My code:

@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.access1300ドル(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.access1300ドル(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);
Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Oct 30, 2014 at 11:42
4
  • May be your Image is too high resolution. Just decode your image using scalesize. Commented Oct 30, 2014 at 12:07
  • can you give me a piece of code? Commented Oct 30, 2014 at 12:09
  • Are you getting image from gallery?? Commented Oct 30, 2014 at 12:09
  • yes, i have get image from galery Commented Oct 30, 2014 at 12:10

2 Answers 2

1

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);
 }
answered Oct 30, 2014 at 12:12
Sign up to request clarification or add additional context in comments.

5 Comments

yourimagePath it is URI witch i get from intent?
No its a image path from gallery.
watch my edited question please, i have tried you code
Just check in Log "yourSelectedImage". Is this getting your imagepath properly or not?
@KostyaKhuta Have you checked my edited answer?? And are you getting image at that path? First check my edited answer and let me know.
0

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 :)

answered Oct 30, 2014 at 12:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.