2014-01-10 3 views
-1

현재 Android 앱을 개발 중입니다. 나는 많은 이미지를 포함하고있다. 갤럭시 탭에서 앱을 테스트 할 때 모든 것이 잘 작동합니다. 넥서스 10 응용 프로그램 충돌을 매우 자주에 의한에서 OutOfMemory 오류 :Nexus 10에서는 앱이 다운되지만 Galaxy Tab 3에서는 다운되지 않습니다.

Caused by: java.lang.OutOfMemoryError 

어떻게 그 10 배의 메모리가 넥서스로 될 수 있는가?

것은 나는이 클래스와 이미지를로드 해요 :

public class ImageAdapter extends PagerAdapter { 
Context context; 
public List<String> imgs = new ArrayList<String>(); 
ImageAdapter(Context context){ 
    this.context=context; 
} 

public void setImages(List<String> images){ 
    this.imgs = images; 
} 

@Override 
public int getCount() { 
    return imgs.size(); 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == ((ImageView) object); 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    ImageView imageView = new ImageView(context); 
    imageView.setPadding(0, 0, 0, 0); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    Drawable d = new BitmapDrawable(context.getResources(), generateBitmap(context, imgs.get(position), 699, 699)); 
    imageView.setImageDrawable(d); 
    ((ViewPager) container).addView(imageView, 0); 
    return imageView; 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    ((ViewPager) container).removeView((ImageView) object); 
} 

public static Bitmap generateBitmap(Context ctx, String path, double maxHeight,double maxWidth) { 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    InputStream bitmapIs = null; 

    DisplayMetrics metrics = ctx.getResources().getDisplayMetrics(); 
    maxHeight = metrics.density * maxHeight; 
    maxWidth = metrics.density * maxWidth; 

    try { 
     bitmapIs = ctx.getAssets().open(path); 
    } catch (Exception e) {e.printStackTrace();} 


    o.inPreferredConfig = Bitmap.Config.RGB_565; 
    BitmapFactory.decodeStream(bitmapIs, null, o); 

    // The new size we want to scale to 
    int REQUIRED_SIZE = 0; 
    if (maxWidth > maxHeight) { 
     REQUIRED_SIZE = (int) maxWidth; 
    } else { 
     REQUIRED_SIZE = (int) maxHeight; 
    } 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth; 
    int height_tmp = o.outHeight; 
    int sampleSize = 1; 
    while (true) { 
     if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     sampleSize++; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = sampleSize; 
    o2.inPreferredConfig = Bitmap.Config.RGB_565; 
    Bitmap originalBmp = BitmapFactory.decodeStream(bitmapIs, null, o2); 

    // Find the correct scale value. It should be the power of 2. 
    int origHeight = originalBmp.getHeight(); 
    int origWidth = originalBmp.getWidth(); 

    double scale = 1.0; 

    double tmpScaleHeight = maxHeight/(double) origHeight; 
    double tmpScaleWidth = maxWidth/(double) origWidth; 

    if (tmpScaleHeight < tmpScaleWidth) { 
     scale = tmpScaleHeight; 
    } else { 
     scale = tmpScaleWidth; 
    } 

    int scaledW = (int) (scale * origWidth); 
    int scaledH = (int) (scale * origHeight); 

    Bitmap tmpBmp = Bitmap.createScaledBitmap(originalBmp, scaledW, 
      scaledH, true); 
    originalBmp.recycle(); 

    return tmpBmp; 
} 

}

01-10 14:14:18.245: E/AndroidRuntime(6992): FATAL EXCEPTION: main 
01-10 14:14:18.245: E/AndroidRuntime(6992): Process: de.abcdesign.abcdesignkatalog2014, PID: 6992 
01-10 14:14:18.245: E/AndroidRuntime(6992): java.lang.OutOfMemoryError 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.content.res.Resources.loadDrawable(Resources.java:2110) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.content.res.Resources.getDrawable(Resources.java:700) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.widget.ImageView.resolveUri(ImageView.java:638) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.widget.ImageView.setImageResource(ImageView.java:367) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at de.abcdesign.abcdesignkatalog2014.Category.onCreate(Category.java:163) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.app.Activity.performCreate(Activity.java:5231) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.os.Handler.dispatchMessage(Handler.java:102) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.os.Looper.loop(Looper.java:136) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at java.lang.reflect.Method.invoke(Method.java:515) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
01-10 14:14:18.245: E/AndroidRuntime(6992):  at dalvik.system.NativeStart.main(Native Method) 
+0

문제가되지 않습니다 Bitmap.Compress 방법을 사용하여 이미지를 압축해야하지만

. 'OutOfMemoryError'를 100 번에서 99 번 얻으면 디바이스가 아닌 앱에 문제가 있습니다 – CodingIntrigue

+0

디바이스에 더 많은 RAM이 있더라도 VM 인스턴스는 여전히 제한된 메모리 예산으로 실행됩니다. – laalto

+0

몇 가지 코드를 게시하여 잘못된 작업을 제안 할 수 있습니다. – Jimmy

답변

0

봅니다 코드 아래 사용하여 이미지의 크기를 줄일 수 있습니다. 그것은 enter code here

  Bitmap myBitmap; 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPurgeable = true; 
    options.outHeight = 50; 
    options.outWidth = 50; 
    options.inSampleSize = 4; 

    myBitmap = BitmapFactory.decodeFile(path, options); 

    // Bitmap bitmap = BitmapFactory.decodeFile(bean); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    myBitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
0

매니페스트에서 true로 재산 largeheap 세트를 추가 나를 위해 메모리 오류 부족을 극복 할 것이다. 그러면이 오류가 발생하지 않습니다. 당신은 또한 않는 아닌지

+0

감사합니다. 문제는 위의 클래스에 Bitmap.Compress를 추가해도 아무 효과가 없다는 것입니다. generateImage 함수에서 tmpBmp를 적용하려고했습니다. – Patricks

관련 문제