【问题标题】:How to show images from assets or internal storage in a gallery to pick from?如何在图库中显示来自资产或内部存储的图像以供选择?
【发布时间】:2015-11-11 15:48:17
【问题描述】:

我目前正在寻找一种让用户从我的应用数据中选择图像的方法。图片目前位于 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. 有没有办法更改该代码以显示来自应用资产文件夹或其内部存储的图像,而不是用户公共画廊文件?

【问题讨论】:

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


    【解决方案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;
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      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
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-27
        相关资源
        最近更新 更多