【问题标题】:getting images from external sd card folder and displaying them in a list从外部 sd 卡文件夹中获取图像并将它们显示在列表中
【发布时间】:2012-05-16 13:07:02
【问题描述】:

我试图从外部SD card 内的特定文件夹中获取图像,并在运行应用程序时尝试将它们显示在列表中。没有提供错误,没有任何反应。任何建议都只是空白页。

我正在获取带有扩展名的图像列表,但我如何查看图像!

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        File file = new File("/sdcard/external_sd/folder_name/");

        File imageList[] = file.listFiles();
        ArrayList<Bitmap> images = new ArrayList<Bitmap>();
        for(int i=0;i<imageList.length;i++)
        {
            Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());

            Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());

            images.add(b);

        }
         setListAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,imageList));
    }

【问题讨论】:

  • 您确定位图解码正确吗?也可以考虑使用 new File(Environment.getExternalStorageDirectory(),"文件夹名称")。
  • setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,imageList));我正在获取带有扩展名的图像列表,我怎样才能让它们出现?作为图像
  • Tony the Pony 这里是 listView ..

标签: android


【解决方案1】:

如果您使用代码加载图像,您将面临OutOfMemory 异常。尝试使用以下方法正确加载图像。之后,您可以填写您的ListView

private static final int IMG_BUFFER_LEN = 16384;
// R.drawable.ic_default - default drawable resource, if we cannot load drawable from provided file
// R.dimen.list_icon_size - size in dp of ImageView inside of ListView (for example, 40dp)

private Drawable extractMediaIcon(String filePath) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    if (options.outWidth < 0 || options.outHeight < 0 || (options.outWidth * options.outHeight == 0)) {
        return context.getResources().getDrawable(R.drawable.ic_default);
    }

    final int targetHeight = dp2px((int) context.getResources().getDimension(R.dimen.list_icon_size));
    final int targetWidth = targetHeight;

    final boolean isScaleByHeight =
        Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth - targetWidth);

    if (options.outHeight * options.outWidth * 2 >= IMG_BUFFER_LEN) {
        // Load, scaling to smallest power of 2 that'll get it <= desired dimensions
        final double sampleSize = isScaleByHeight
                ? options.outHeight / targetHeight
                : options.outWidth / targetWidth;
        options.inSampleSize =
                (int) Math.pow(2d, Math.floor(
                        Math.log(sampleSize) / Math.log(2d)));
    }

    // Do the actual decoding
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[IMG_BUFFER_LEN];

    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
    if (bitmap == null) {
        return context.getResources().getDrawable(R.drawable.ic_default);
    }

    final double imageFactor = (double) bitmap.getWidth() / bitmap.getHeight();
    final double targetFactor = (double) targetWidth / targetHeight;

    bitmap = Bitmap.createScaledBitmap(
            bitmap,
            targetFactor > imageFactor ? bitmap.getWidth() * targetHeight / bitmap.getHeight() : targetWidth,
            targetFactor > imageFactor ? targetHeight : bitmap.getHeight() * targetWidth / bitmap.getWidth(),
            false);

    return new BitmapDrawable(context.getResources(), bitmap);
}

【讨论】:

  • StenaviN 我试过你的方法对我不起作用我需要预定义文件路径..
  • 当然,调用extractMediaIcon(...)方法,提供sdcard上图片文件的路径,取回Drawable
  • StenaviN 能给我提供一个 setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,..));您的代码中的第二个参数应该是什么!我找不到显示图像的方法,你能帮帮我吗
  • 我不推荐使用标准适配器。我总是使用自定义的并通过“持有人”模式进行优化。了解更多 herehere。例子很多。
  • 我设法获取了位于我的画廊文件夹中的所有图像我如何过滤内容以便仅获取特定文件夹的内容我正在使用此光标的任何想法 = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, MediaStore.Images.Thumbnails._ID, // 哪些列返回 null, // 将所有行返回 null, MediaStore.Images.Thumbnails.IMAGE_ID);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多