Android ImageAdapter setImageResource

时间:2013-04-12 06:01:43

标签: android imageview

以下是我的代码。我正在尝试从我的SD卡中检索图像到gridview。我的代码有问题。我无法弄清楚如何将图像设置到我的imageview。我得到错误“ImageView中的方法setImageResource(int)不适用于我的ImageAdapter中的参数(String)”

public class GridViewActivity extends Activity {
    private String[] mFileStrings;
    private File[] listFile;
    GridView grid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set title for the GridView
        setTitle("GridView");
        setContentView(R.layout.grid_view);

        File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + "Tutorial");

        if (file.isDirectory()) {
            listFile = file.listFiles();
            mFileStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++) {
                mFileStrings[i] = listFile[i].getAbsolutePath();
            }
        }

        grid = (GridView) findViewById(R.id.gridview);

        grid.setAdapter(new ImageAdapter(this, mFileStrings));
    }

ImageAdapter

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private String[] data;
    public ImageAdapter(Context c, String[] d) {
        mContext = c;
        data=d;
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

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

    // Create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // If it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(data[position]); //The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)
        return imageView;
    }


}

我的编辑

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        File imgFile = new File("" + data[position]);
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        imageView.setImageBitmap(myBitmap);
        return imageView;

    }

LOGCAT:

04-12 14:18:59.811: E/AndroidRuntime(27379): FATAL EXCEPTION: main
04-12 14:18:59.811: E/AndroidRuntime(27379): java.lang.OutOfMemoryError
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at com.androidbegin.gridview.ImageAdapter.getView(ImageAdapter.java:46)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.widget.AbsListView.obtainView(AbsListView.java:2201)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.widget.GridView.onMeasure(GridView.java:1026)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.view.View.measure(View.java:12892)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.view.View.measure(View.java:12892)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.view.View.measure(View.java:12892)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2265)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.view.View.measure(View.java:12892)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1624)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.os.Looper.loop(Looper.java:137)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at android.app.ActivityThread.main(ActivityThread.java:4507)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at java.lang.reflect.Method.invokeNative(Native Method)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at java.lang.reflect.Method.invoke(Method.java:511)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
04-12 14:18:59.811: E/AndroidRuntime(27379):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

您正在设置imageView.setImageResource(data [position]),其中data是字符串数组。 您需要将文件路径解码为位图,然后将其设置为您的imageview。

getView()

File imgFile = new  File(""+data[position]);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);

编辑:

您还可以使用LazyList或Universal Image Loader来显示SD卡中的图像。 What's LazyList?。 Lazy List缩小以减少内存消耗。 Universal Image Loader的工作原理与LazyList相同,但它有更多的配置选项。

https://github.com/thest1/LazyList

https://github.com/nostra13/Android-Universal-Image-Loader

  //decodes image and scales it to reduce memory consumption
  private Bitmap decodeFile(File f){
     try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

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

        BitmapFactory.Options o2 = new   BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
   } catch (FileNotFoundException e) {}
    return null;
}

如果位图太大,则需要缩放它们,然后在imageview中显示相同的位图。此外,您应该使用视图支架来实现平滑滚动和性能。您应该在不使用时回收位图。 gridview中的可见视图不会被回收。您可以使用LazyList或Universal Image Loader。

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

http://www.youtube.com/watch?v=wDBM6wVEO70。谈话是关于观看者和列表视图的表现。同样适用于gridview。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html。有效加载位图。

如果遇到内存泄漏,请使用MAT Analyzer查找并修复问题。 http://www.youtube.com/watch?v=_CruQY55HOk

答案 1 :(得分:0)

您必须在适配器中执行此操作

File f = new File("/mnt/sdcard/photo.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);

在这里你必须做File f = new File(""+data[position]);

已修改:您将获得OutOfMemoryError因此,我会哄骗您..

How do I do a lazy load of images in ListView

检查上述链接的所有答案。

答案 2 :(得分:0)

用于管理Local file Path Online url pathBaseAdpter lazy Loading BaseAdpter更有助于See

只需要关注ImageLoader imageLoader=new ImageLoader(context); ... imageLoader.DisplayImage(url, imageView); 关于

{{1}}