安卓:显示图像从SD卡 [英] Android: Displaying Images From SDcard

查看:151
本文介绍了安卓:显示图像从SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在市场上显示的所有照片从用户在画廊SD卡的应用程序。起初我了内存的问题,但通过在BitmapFactory选项增加inSampleSize缓解的那些。不过,我仍然没有显示图像的间歇性报告,在某些情况下根本没有。人们似乎有问题的Droid Eris的用户比例较大。下面是加载所有的照片和随行ImageAdapter方法​​:

 私人无效loadAllPhotos(){
    的setContentView(R.layout.add_pictures);
    的String [] =投影{MediaStore.Images.Thumbnails._ID};
    光标= managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            投影,
            空值,
            空值,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
    INT大小= cursor.getCount();
    如果(大小== 0){
        Toast.makeText(thisContext,无法在SD卡找到照片。Toast.LENGTH_SHORT).show();
    }
    G =(图库论坛)findViewById(R.id.gallery);
    g.setAdapter(新ImageAdapter(本));
}

该ImageAdapter:

 公共类ImageAdapter延伸BaseAdapter {
    INT mGalleryItemBackground;
    公共ImageAdapter(上下文C){
        mContext = C;
        TypedArray A = obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
        a.recycle();
    }
    公众诠释的getCount(){
     INT cursorCount = mCursor.getCount();
      返回cursorCount;
    }
    公共对象的getItem(INT位置){
        返回的位置;
    }
    众长getItemId(INT位置){
        返回的位置;
    }
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
      ImageView的I =新ImageView的(mContext);
      System.gc()的;
      如果(convertView == NULL){
         尝试{
             的String [] = PROJ {MediaStore.Images.Media.DATA};
             mCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,凸出,NULL,NULL,NULL);
             与Column_Index = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
             mCursor.moveToPosition(位置);
             文件名= mCursor.getString(Column_Index中);
             如果(filename.endsWith(JPG)|| filename.endsWith(PNG)){
                 BitmapFactory.Options选项=新BitmapFactory.Options();
                 options.inSampleSize = 15;
                 位图BM = BitmapFactory.de codeFILE(文件名,期权);
                 i.setImageBitmap(BM);
                 i.setScaleType(ImageView.ScaleType.FIT_XY);
                 i.setLayoutParams(新Gallery.LayoutParams(150,150));
                 i.setBackgroundResource(mGalleryItemBackground);
                 System.gc()的;
             }
         }赶上(例外五){
         }
      }
           返回我;
    }
  }


解决方案

如果您还没有这样做的话,集成乱舞或DroidDrop或东西收集例外,因为它们可能会阐明你的问题的一些情况。

还有:


  1. 请回收您的查看!您正在创建一个新的的ImageView getView()调用,而不是帮助你的GC的情况。

  2. 您拨打的同一 managedQuery() getView()调用,这是可怕的。这样做,有一次,请。

  3. 您似乎在访问 mCursor getCount将()之前在初始化getView()

  4. 与Column_Index 应始终 0 在您所呼叫 managedQuery()的方式,所以你不应该需要使用 getColumnIndexOrThrow()。即使你想使用,只有这样做一次,当你让你的单 managedQuery()电话。

无论任何,将清理你的Droid Eris的问题,虽然,我不能说。

I have an app on the market that displays all photos from the user's SDcard in a gallery. Initially I had out of memory issues, but alleviated those by increasing the inSampleSize in the BitmapFactory options. However, I still have intermittent reports of images not displaying, in some cases none at all. There seems to be a larger proportion of Droid Eris users having problems. Here is the method that loads all photos and the accompanying ImageAdapter:

    private void loadAllPhotos() {
    setContentView(R.layout.add_pictures);      
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, 
            null,       
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
    int size = cursor.getCount();
    if (size == 0) {
        Toast.makeText(thisContext, "Can't find pics on SDcard.", Toast.LENGTH_SHORT).show();
    }
    g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));  
}

The ImageAdapter:

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }
    public int getCount() {
     int cursorCount = mCursor.getCount();
      return cursorCount;
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
      ImageView i = new ImageView(mContext);
      System.gc();
      if (convertView == null) {                                        
         try {
             String [] proj={MediaStore.Images.Media.DATA};
             mCursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,proj, null, null, null); 
             column_index = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
             mCursor.moveToPosition(position);
             filename = mCursor.getString(column_index);
             if (filename.endsWith(".jpg") || filename.endsWith(".png")) {
                 BitmapFactory.Options options = new BitmapFactory.Options();
                 options.inSampleSize = 15;
                 Bitmap bm = BitmapFactory.decodeFile(filename, options);
                 i.setImageBitmap(bm);
                 i.setScaleType(ImageView.ScaleType.FIT_XY);
                 i.setLayoutParams(new Gallery.LayoutParams(150, 150));
                 i.setBackgroundResource(mGalleryItemBackground); 
                 System.gc();
             }
         } catch (Exception e) { 
         } 
      }
           return i;
    } 
  } 

解决方案

If you have not done so already, integrate Flurry or DroidDrop or something to collect exceptions, as they may shed some light on your problem.

Also:

  1. Please recycle your Views! You are creating a new ImageView on every getView() call, which is not helping your GC situation.
  2. You are calling the same managedQuery() on every getView() call, which is horrible. Do that once, please.
  3. You seem to be accessing mCursor in getCount() before it is initialized in getView().
  4. column_index should always be 0 in the way you are calling managedQuery(), so you should not need to use getColumnIndexOrThrow(). Even if you want to use that, only do that once, when you make your single managedQuery() call.

Whether any of that will clear up your Droid Eris problems, though, I cannot say.

这篇关于安卓:显示图像从SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆