如何从库中的资源或内部存储中显示图像以供选择?

时间:2015-11-11 15:48:18

标签: android android-intent gallery android-gallery internal-storage

我目前正在寻找让用户从我的应用数据中选择图片的方法。图像当前位于Assets文件夹中,但如果更容易,可能会被推送到内部存储区。

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

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

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. 有没有办法更改该代码以显示apps资源文件夹或其内部存储中的图像,而不是用户公共图库文件?

2 个答案:

答案 0 :(得分:1)

我自己使用GridView创建了一个“图库”。

我使用了那边的代码来创建一个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;
    }
}

答案 1 :(得分:0)

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

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
    }