2

good morning i tried to scroll a large image in my android app the size (800*3782, 1.7 Mb png file) i used this documentation https://developer.android.com/training/displaying-bitmaps/load-bitmap.html but every time i had the same error :

08-28 11:00:36.534: E/AndroidRuntime(2143): Caused by: java.lang.OutOfMemoryError
08-28 11:00:36.534: E/AndroidRuntime(2143):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-28 11:00:36.534: E/AndroidRuntime(2143):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
08-28 11:00:36.534: E/AndroidRuntime(2143):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
08-28 11:00:36.534: E/AndroidRuntime(2143):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
08-28 11:00:36.534: E/AndroidRuntime(2143):     at android.content.res.Resources.loadDrawable(Resources.java:1965)
08-28 11:00:36.534: E/AndroidRuntime(2143):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-28 11:00:36.534: E/AndroidRuntime(2143):     at android.widget.ImageView.<init>(ImageView.java:120)
08-28 11:00:36.534: E/AndroidRuntime(2143):     at android.widget.ImageView.<init>(ImageView.java:110)
08-28 11:00:36.534: E/AndroidRuntime(2143):     ... 28 more

this the hole code : about.java

public class About extends Activity {

    ImageView mImageView;    //reference to the ImageView
    int xDim, yDim;        //stores ImageView dimensions

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
        //attach an instance of HandleClick to the Button

        //reference the ImageView
        mImageView=(ImageView)findViewById(R.id.imageView1);
        mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.aboutscroll, 800, 1200));
    }


    @Override
    //Get the size of the Image view after the 
    //Activity has completely loaded
    public void onWindowFocusChanged(boolean hasFocus){
        super.onWindowFocusChanged(hasFocus);
        xDim=mImageView.getWidth();
        yDim=mImageView.getHeight();
    }

    //Load a bitmap from a resource with a target size
    static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    //Given the bitmap size and View size calculate a subsampling size (powers of 2) 
    static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int inSampleSize = 1;    //Default subsampling size
        // See if image raw height and width is bigger than that of required view
        if (options.outHeight > reqHeight || options.outWidth > reqWidth) {
            //bigger
            final int halfHeight = options.outHeight / 2;
            final int halfWidth = options.outWidth / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

}

this is the xml file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" />
        </ScrollView>

</LinearLayout>

it's work only when i scale image to 100*100 px witch isn't my target i would like to scroll all the image without scaling i test the app in nexus 7 emulator i hope that someone help me and think you

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Walid Sassi
  • 75
  • 1
  • 2
  • 11

1 Answers1

1

You can increase the partition size as below

-partition-size 1024

Check the accepted answer here for more details

How to increase storage for Android Emulator? (INSTALL_FAILED_INSUFFICIENT_STORAGE)

Also, did you try to test it on a device?

Community
  • 1
  • 1
Ajit Pratap Singh
  • 1,299
  • 12
  • 24