【问题标题】:How do I open a file I have the Uri for in Android?如何在 Android 中打开我拥有 Uri 的文件?
【发布时间】:2015-03-08 15:31:15
【问题描述】:

我正在尝试打开存储在外部存储器中的图像。这是我的代码:

File imagePath = new File(imageURI);
InputStream inputStream=null;
try {
    inputStream = getContentResolver().openInputStream(Uri.parse(imageURI));
}catch(FileNotFoundException e){
    e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] originalImage = baos.toByteArray();

但它似乎无法找到该文件。 Uri 的格式为 content://com.android.providers.media.documents/document/image%3A21。 感谢您的帮助。

【问题讨论】:

    标签: java android image file


    【解决方案1】:

    在我正在处理的一个项目中,我在 SD 卡上的目录中有外部图像。我正在使用

    String thePath = Environment.getExternalStorageDirectory() + "/myAppFiles”;
            File imgFile = new File(thePath + " / " + "
    externalImage.jpg ");
            if (imgFile.exists()) {
                try {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 6;
                    Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
    
                }
            }

    【讨论】:

      【解决方案2】:

      试试这个:

      File imagePath = new File(imageURI.getPath());
      

      url.getPath() 返回一个String,格式如下:“/mnt/sdcard/xxx.jpg”,不带scheme type前缀

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-11
        • 2014-04-19
        • 2011-08-14
        • 1970-01-01
        相关资源
        最近更新 更多