2014-08-28 57 views
2

早上好,我試圖在我的Android應用程序滾動一個大的圖像尺寸(800 * 3782,1.7 MB png文件)我用這個文檔https://developer.android.com/training/displaying-bitmaps/load-bitmap.html 但每次我有同樣的錯誤時間:imageview的機器人了內存

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 

此孔代碼: 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; 
    } 

} 

這是xml文件:

<?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> 

它的工作只有當我縮放圖像到100×100像素女巫不是我的目標我想滾動所有圖像沒有縮放我測試應用程序在nexus 7模擬器 我希望有人幫助我,想你

+0

檢查:http://developer.android.com/training/displaying-bitmaps/index.html – 2014-08-28 11:12:16

+0

通常,當我在模擬器上測試一個具有大圖像的應用程序時,每次都會崩潰,無論使用什麼內存規格。你是否在設備上測試你的應用程序? ... 800 * 3782?非常巨大! – MikeKeepsOnShine 2014-08-28 11:15:21

+0

你應該在設備上進行測試。 Android模擬器表現糟糕。檢查它的下端設備,看看它是否工作 – humblerookie 2014-08-28 11:23:15

回答

1

您可以增大分區大小如下

-partition-size 1024 

檢查接受的答案這裏查看更多詳情

How to increase storage for Android Emulator? (INSTALL_FAILED_INSUFFICIENT_STORAGE)

另外,你是否嘗試在設備上測試它?

+0

不,我沒有在設備上試過 – 2014-08-28 11:30:51

+0

我會增加分區大小並在設備上測試 – 2014-08-28 11:32:30

+0

我增加了設備分區,但同樣的錯誤 – 2014-08-28 11:46:58