0

I am trying to show SD Card image into ImageView and getting OutOfMemory error.

Please let me know How can i show SDCard's images into ImageView (no matter how big they are.)

I know we can use, these properties to control on it, but i am looking for the best solution :

    android:hardwareAccelerated="false"
    android:largeHeap="true"

Logcat:

08-05 11:27:34.530: E/AndroidRuntime(8158): FATAL EXCEPTION: main
08-05 11:27:34.530: E/AndroidRuntime(8158): java.lang.OutOfMemoryError
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:449)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at com.physic.UploadActivity.previewMedia(UploadActivity.java:171)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at com.physic.UploadActivity.onCreate(UploadActivity.java:109)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.Activity.performCreate(Activity.java:5191)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.os.Looper.loop(Looper.java:137)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at android.app.ActivityThread.main(ActivityThread.java:4921)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
08-05 11:27:34.530: E/AndroidRuntime(8158):     at dalvik.system.NativeStart.main(Native Method)

Code:

/**
     * Displaying captured image/video on the screen
     * */
    private void previewMedia(boolean isImage) {
        // Checking whether captured media is image or video
        if (isImage) {
            imgPreview.setVisibility(View.VISIBLE);

            final Bitmap bitmap = BitmapFactory.decodeFile(filePath);           

            ExifInterface exif = null;
            try {
                exif = new ExifInterface(filePath);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                    ExifInterface.ORIENTATION_UNDEFINED);

            Bitmap bmRotated = rotateBitmap(bitmap, orientation);
            imgPreview.setImageBitmap(bmRotated);

        } else {
            imgPreview.setVisibility(View.GONE);
        }

    }

Using Picasso Lib:

        Picasso.with(UploadActivity.this)
        .load(new File(filePath)).rotate(90)
        .fit().centerInside()
        .error(R.drawable.ic_launcher)
        .placeholder(R.drawable.ic_launcher)
        .into(imgPreview);

And even tried with this:

        .transform(transformation)

Log:

Log.d("file:", filePath);
D/file:(6201): /storage/sdcard0/Pictures/Testing/LICPolicy20150805120029.jpg

Tried with Picasso lib but not showing image preview ..

Matt
  • 74,352
  • 26
  • 153
  • 180
Sophie
  • 2,594
  • 10
  • 41
  • 75
  • You should use ImageLoading library which will handle memory issues itself. – Karn Shah Aug 05 '15 at 06:14
  • 1
    Also you can read the official guide: http://developer.android.com/intl/ru/training/displaying-bitmaps/load-bitmap.html – daemmie Aug 05 '15 at 06:17
  • you can use libraries like picasso to show images from sd card. check this http://stackoverflow.com/questions/24097576/how-to-load-image-from-sd-card-using-picasso-library – karan Aug 05 '15 at 06:22
  • @Sophie you need to use `load(new File(path))` instead of this `load(filePath)` – karan Aug 05 '15 at 06:45
  • @KaranMer still not able to load image into ImageView – Sophie Aug 05 '15 at 07:04
  • @Sophie is your image located at path you are passing check that. you can get your image from sdcard using below code `String filename = "YOURIMAGE.png"; String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); Picasso.with(context).load(new File(baseDir + File.separator + filename)).into(imageView);` – karan Aug 05 '15 at 07:11
  • @KarnShah i tried with picasso lib but still not able to load image into ImageView – Sophie Aug 05 '15 at 07:11
  • 1
    Maybe the Visibility of imgPreview is set to GONE. ^^ – daemmie Aug 05 '15 at 07:11
  • 1
    show proper code snippet where you used picasso – Karn Shah Aug 05 '15 at 07:13
  • @KaranMer please check above code – Sophie Aug 05 '15 at 07:20
  • what is your toast showing, can you post pic – karan Aug 05 '15 at 07:24
  • the exact name along with path – Sophie Aug 05 '15 at 07:26
  • Don't hard code sd card path use Environment.getExternalStorageDirectory() instead. – Karn Shah Aug 05 '15 at 07:27
  • make sure there is no exception in your catch try logging this Log.e("exception",": ",e) – karan Aug 05 '15 at 07:27
  • just printout exception in catch. may be you are getting any exception like file not found – Karn Shah Aug 05 '15 at 07:28
  • @KaranMer sorry friend Visibility of imgPreview was set to GONE :) – Sophie Aug 05 '15 at 07:32
  • @KarnShah I would like to know why i am not getting neither OutOfMemory exception nor bitmap too large exception when using Picasso ! – Sophie Aug 05 '15 at 07:47
  • @Sophie: Please do not fundamentally change your question, so that it invalidates the provided answers. Ask a new question instead. – Matt Aug 05 '15 at 17:57

1 Answers1

1

OutOfMemoryException occur when your image is too large and your device is not able to render it so what you can do is to down scale the image according to your need below is the code for downscaling

 public static Bitmap compressed_Bitmap(Bitmap bitmap) {
    final int maxSize = 700;
    int outWidth;
    int outHeight;
    int inWidth = bitmap.getWidth();
    int inHeight = bitmap.getHeight();
    if (inWidth > inHeight) {
        outWidth = maxSize;
        outHeight = (inHeight * maxSize) / inWidth;
    } else {
        outHeight = maxSize;
        outWidth = (inWidth * maxSize) / inHeight;
    }

    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false);
    return resizedBitmap;
}
Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26