2013-08-10 3 views
0

adapter - getView() 메소드를 사용하여 sdcard0에서 모든 이미지를 검색하려고합니다. 갤러리 뷰를 채우려고하는데, 요구 사항 문서는 갤러리 뷰를 사용한다는 것을 암시합니다. 내가 볼 수 있듯이 이미지가 거의 없다면이 접근법은 작동하지만 sdcard0에 10 개 이상의 이미지가있는 경우 OutOfMemoryException이 발생합니다. 누군가가 내가 파일에서 읽고 난 메모리가 부족 해요 비트 맵 객체를 많이 만드는거야 때문에BitmapFactory는 sdcard0에서 모든 이미지를 읽을 때 OutOfMemoryError를 표시합니다.

"Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile((file.getPath())), 275, 210);". 

을 만드는거야 비트 맵 객체의 메모리를 재사용하는 데 도움이 있습니다. 이미지는 모두 jpg 형식이며 장치 카메라를 사용하여 클릭하고 3.5MB의 평균 크기입니다. 그것의 삼성 갤럭시 S4는 내가 테스트 중이 야.

Button btnFetch; 
ImageView ivLoad; 
Gallery galView; 
MyAdapter adapter; 
static Context context; 
File[] files; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sdcard); 
    context = SDCardActivity.this; 
    btnFetch = (Button) findViewById(R.id.btnFetch); 
    ivLoad = (ImageView) findViewById(R.id.ivLoad); 
    galView = (Gallery) findViewById(R.id.galView); 

    loadingMyGalleryView(); 
    adapter = new MyAdapter(); 
    galView.setAdapter(adapter); 

    galView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      File file = files[position]; 
      Bitmap bm = BitmapFactory.decodeFile(file.getPath()); 
      ivLoad.setImageBitmap(bm); 
     } 
    }); 
} 

private void loadingMyGalleryView() { 
    if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_MOUNTED)) { 

     // It have to be matched with the directory in SDCard 
     File f = new File(Environment.getExternalStorageDirectory() 
       + File.separator + "DCIM/Camera"); 
     boolean exist = f.exists(); 
     Log.d("loki", "Reached Line 80" + exist); 
     if (exist) { 
      Log.d("loki", "Reached line 82"); 
      files = f.listFiles(); 
      for (int i = 0; i < files.length; i++) { 
       Log.d("loki", "" + files[i]); 
      } 
     } 
    } 
} 

class MyAdapter extends BaseAdapter { 

    @Override 
    public Object getItem(int position) { 
     return files[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public int getCount() { 
     // Log.d("loki", ""+files.length); 
     return files.length; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ImageView image = new ImageView(context); 
     File file = files[position]; 
     Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile((file.getPath())), 275, 210); 
     image.setImageBitmap(bitmap); 
     image.setAdjustViewBounds(true); 
     image.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     return image; 
    } 
} 

로그 캣을 보여줍니다 : 올바르게 convertView을 처리하지 않는 때문에 각 행에 대해 (당신이 여기 아무것도하지 않는 사실 경우) 기존 재사용하는 대신 새로운 뷰를 생성

08-10 12:15:38.403: E/AndroidRuntime(21585): FATAL EXCEPTION: main 
08-10 12:15:38.403: E/AndroidRuntime(21585): java.lang.OutOfMemoryError 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:302) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:328) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at com.example.sdcard.SDCardActivity$MyAdapter.getView(SDCardActivity.java:140) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.View.measure(View.java:15518) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.View.measure(View.java:15518) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.View.measure(View.java:15518) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.View.measure(View.java:15518) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2313) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.View.measure(View.java:15518) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.Choreographer.doFrame(Choreographer.java:532) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.os.Handler.handleCallback(Handler.java:725) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.os.Handler.dispatchMessage(Handler.java:92) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.os.Looper.loop(Looper.java:137) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at android.app.ActivityThread.main(ActivityThread.java:5195) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at java.lang.reflect.Method.invokeNative(Native Method) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at java.lang.reflect.Method.invoke(Method.java:511) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 
08-10 12:15:38.403: E/AndroidRuntime(21585): at dalvik.system.NativeStart.main(Native Method) 

답변