如何在图库中显示来自资产或内部存储的图像以供选择?

How to show images from assets or internal storage in a gallery to pick from?

我目前正在寻找一种方法让用户从我的应用程序数据中选择图像。图片目前位于 Assets 文件夹中,但如果方便的话,可以将其推送到内部存储空间。

我不希望所有文件都存储在 public 存储中。

我正在使用以下代码从用户图库中进行选择,效果很好。我希望以某种方式针对当前情况使用类似的代码。

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
  1. 有没有办法更改该代码以显示来自应用资产文件夹或其内部存储的图像,而不是用户 public 图库文件?

您应该尝试一下,并确保您的设备上安装了任何文件资源管理器应用程序。

Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/yourFolder/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");

if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
    {
        startActivity(intent);
    }
else 
    {
      // if you reach this place, it means there is no any file 
      // explorer app installed on your device
    }

我自己用 GridView 创建了一个 "Gallery"。

我使用那边的代码创建了一个 ImageAdapter,并做了一些改动:

 public class ImageAdapter extends BaseAdapter {

    private ArrayList <String> data = new ArrayList();

    // I'm using a yamlReader to fill in the data, but it could instead just be hardcoded.
    fillDataWithImageNames();

    // 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.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        // The images are in /app/assets/images/thumbnails/example.jpeg
        imageView.setImageDrawable(loadThumb("images/thumbnails/" + data.get(position) + ".jpeg"));
        return imageView;
    }

    // references to our images
    private Drawable loadThumb(String path) {
    try {
        // get input stream
        InputStream ims = mContext.getAssets().open(path);
        // load image as Drawable
        Drawable d = Drawable.createFromStream(ims, null);
        // set image to ImageView
        return d;
    }
    catch(IOException ex) {
        return null;
    }
}