使用文件路径创建图库的问题不是可绘制的图像

时间:2011-12-30 19:33:54

标签: android

所以我试图建立一个图库,显示SD卡上文件夹中的图像,而不是我可绘制文件夹中的图像。我有一个文件路径数组,我试图从库中的每个图像使用文件uri但是我得到一个运行时错误,导致应用程序在此行崩溃:

iView.setImageURI(Uri.fromFile(new File(files.get(arg0))));

以下是LogCat中错误的一部分:(它很大,所以我没有包含整个内容)

12-30 14:26:56.600: E/AndroidRuntime(4995): FATAL EXCEPTION: main
12-30 14:26:56.600: E/AndroidRuntime(4995): java.lang.OutOfMemoryError
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:738)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.drawable.Drawable.createFromStream(Drawable.java:698)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.ImageView.resolveUri(ImageView.java:530)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.ImageView.setImageURI(ImageView.java:314)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at com.cp.Media$ImageAdapter.getView(Media.java:174)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.Gallery.makeAndAddView(Gallery.java:748)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.Gallery.fillToGalleryRight(Gallery.java:700)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.Gallery.layout(Gallery.java:631)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.Gallery.onLayout(Gallery.java:339)

以下是我的代码,有人知道我做错了吗?

    ImageSwitcher iSwitcher;
    private ArrayList<String> files = new ArrayList<String>();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    setContentView(R.layout.media);
     files.add("/mnt/sdcard/DCIM/Camera/IMG_20111124_130713.jpg");
            files.add("/mnt/sdcard/DCIM/Camera/IMG_20111031_072817.jpg");
            files.add("/mnt/sdcard/DCIM/Camera/IMG_20111031_072750.jpg");
    iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
            iSwitcher.setFactory(this);
            iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                    android.R.anim.fade_in));
            iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                    android.R.anim.fade_out));

            Gallery gallery = (Gallery) findViewById(R.id.Gallery01);
            gallery.setAdapter(new ImageAdapter(this));
            gallery.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    //iSwitcher.setImageResource(Uri.fromFile(new File(files.get(arg2))));
                    iSwitcher.setImageURI(Uri.fromFile(new File(files.get(arg2))));
                }
            });
    }

    public class ImageAdapter extends BaseAdapter {

            private Context ctx;

            public ImageAdapter(Context c) {
                ctx = c; 
            }

            @Override
            public int getCount() {

                return files.size();
            }

            @Override
            public Object getItem(int arg0) {

                return arg0;
            }

            @Override
            public long getItemId(int arg0) {

                return arg0;
            }

            @Override
            public View getView(int arg0, View arg1, ViewGroup arg2) {

                ImageView iView = new ImageView(ctx);

//THE LINE BELOW IS THE LINE THAT CAUSES THE ERROR IN LOGCAT
                iView.setImageURI(Uri.fromFile(new File(files.get(arg0))));
                iView.setScaleType(ImageView.ScaleType.FIT_XY);
                iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
                return iView;
            }

        }

        @Override
        public View makeView() {
            ImageView iView = new ImageView(this);
            iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            iView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
            iView.setBackgroundColor(0xFF000000);
            return iView;
        }

1 个答案:

答案 0 :(得分:1)

这个想法是从SD卡上的文件创建一个更轻的位图。看看这个链接:http://www.stackoverflow.com/a/823966/935075