2012-11-05 6 views
3

책 읽기 용으로 Android 용 앱을 하나 개발합니다. 책 페이지와 오디오는 Amazon Bucket에서 다운로드 할 수 있습니다. 다운로드 후 SD 카드에 저장합니다.android - 메모리 부족 예외

다음 및 이전 페이지에는 두 개의 버튼이 있습니다. 페이지를 표시 할 때 메모리 부족 예외가 발생합니다. 여기

11-03 10:59:39.199: E/AndroidRuntime(13566): FATAL EXCEPTION: main 
11-03 10:59:39.199: E/AndroidRuntime(13566): java.lang.OutOfMemoryError 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.graphics.drawable.Drawable.createFromPath(Drawable.java:880) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at org.Infoware.childrenbible.GeneralHelper.setImgfromSDCard(GeneralHelper.java:608) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at org.Infoware.childrenbible.CoverPageActivity.onCreate(CoverPageActivity.java:205) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.app.Activity.performCreate(Activity.java:4465) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.os.Looper.loop(Looper.java:137) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at android.app.ActivityThread.main(ActivityThread.java:4424) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at java.lang.reflect.Method.invokeNative(Native Method) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at java.lang.reflect.Method.invoke(Method.java:511) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
11-03 10:59:39.199: E/AndroidRuntime(13566): at dalvik.system.NativeStart.main(Native Method) 

그것은 내 자바 코드 :

public class PageReadActivity extends Activity 
{ 
    Bitmap bitmap; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
     super.onCreate(savedInstanceState); 
    try 
    { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.pageread_layout); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
     Log.d(TAG, "Error in onCreate"); 
    } 
} 

    @Override 
    protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    displayPage(product_code, page_Index); 

     btnNext.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
          page_Index = page_Index + 1; 
       displayPage(product_code, page_Index); 

      } 
     }); 

     btnPrevious.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
      page_Index = page_Index - 1; 
    displayPage(product_code, page_Index); 

      } 
     }); 

    } 

     private void displayPage(String product_code, int ind) 
{ 
    try 
    { 
     String imageName = product_code + ind + ".jpg"; 

     String imgFilePath = objHelper.getFullFilePath(imageName); 

     File imageFile = new File(imgFilePath); 

     imgViewPage = new ImageView(myContext); 

     if(imageFile.exists()) 
     { 

      bitmap = objHelper.decodeSampledBitmapFromResource(imageName,objHelper.screenWidth,objHelper.screenHeight);    

      imgViewPage.setImageBitmap(bitmap); 
      bitmap = null; 

      String audioName = product_code+"_audio" + ind + ".mp3"; 
      String audioFilePath = objHelper.getFullFilePath(audioName); 

      File audioFile = new File(audioFilePath); 

      if(audioFile.exists()) 
      { 
       progressBarWaiting.setVisibility(View.INVISIBLE); 
       stopAudio(); 
       playAudio(ind); 
      } 

     } 
     else 
     { 
      System.out.println("Image not exists....."); 
     }   
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
     Log.e(TAG, "ERROR in displayPage"); 
    } 
} 

    @Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    objHelper.unbindDrawables(findViewById(R.id.pageReadLayout)); 
    System.gc(); 
} 

    public void unbindDrawables(View view) 
{ 
    if(view.getBackground() != null) 
    { 
     view.getBackground().setCallback(null); 
    } 

    if(view instanceof ViewGroup && !(view instanceof AdapterView)) 
    { 
     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) 
     { 
      unbindDrawables(((ViewGroup) view).getChildAt(i)); 
     } 

     ((ViewGroup) view).removeAllViews(); 
    } 
} 

    public Bitmap decodeSampledBitmapFromResource(String fileName,int reqWidth, int reqHeight) 
{ 

    String imagePath = getFullFilePath(fileName); 

    @SuppressWarnings("unused") 
    File imgFile = new File(imagePath); 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(imagePath,options); 

    // Calculate inSampleSize 
    options.inSampleSize = 1; 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 

    return BitmapFactory.decodeFile(imagePath,options); 
} 
    } 
+0

변경이 options.inSampleSize = 4; –

답변

8

비트 맵 이미지의 원래 크기 변환은 X 4. 그래서 안드로이드 앱에 지정되어 더 힙을 차지합니다.

사용이 비트 맵은 더 이상 필요하지 않은 경우

bitmap.recycle(); 
bitmap = null; 
2

내 생각은 비트 맵이 제대로 처리되지 않고 그가에서 OutOfMemoryException을 만드는 것입니다. 비트 맵은 사용하지 않을 때 재활용해야합니다. look here.을 가져 가십시오. 또한 have a look at how to find memory leaks here MAT Analyzer를 사용합니다. 이 둘의 sequre하여 증가 후 작동하지 않는 경우

0

그냥 inSampleSize = 4 변경

+1

나는 이미 그것을했다. 그러나 이미지 품질이 좋지 않습니다. – Mansi