简体   繁体   English

Android:尝试从 Uri 获取图像时出现 FileNotFoundException

[英]Android : FileNotFoundException when trying to get an image from Uri

I am trying to get an image's width and height in order to rezize it in case it is too big before showing it.我正在尝试获取图像的宽度和高度,以便在显示之前调整它的大小,以防它太大。 My method receive an uri of the image but I always get a FileNotFoundException when trying to decode it using BitmapFactory.我的方法接收图像的 uri,但在尝试使用 BitmapFactory 对其进行解码时,我总是收到 FileNotFoundException。

Here is a sample of my code :这是我的代码示例:

    Uri myUri;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(new File(myUri.getPath()).getAbsolutePath(), options);

    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;

Decode File returns throws a FileNotFoundException here.解码文件返回在这里抛出一个 FileNotFoundException。 However, I can still show the image using the uri without using a bitmap factory using this :但是,我仍然可以使用 uri 显示图像,而无需使用位图工厂:

        Uri myUri;

        Bitmap myBitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), myUri);

The problem with this code is that the returned bitmap might cause an OutofMemoryError when using big images.此代码的问题在于,在使用大图像时,返回的位图可能会导致 OutofMemoryError。

When I debug my Uri, I get this :当我调试我的 Uri 时,我得到了这个:

content://com.android.providers.media.documents/document/image%3A20472 content://com.android.providers.media.documents/document/image%3A20472

UPDATE ||更新 || Here is the working code这是工作代码

    Uri myUri;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    // This is the part that changed
    InputStream inputStream = context.getApplicationContext().getContentResolver().openInputStream(myUri);
    BitmapFactory.decodeStream(inputStream,new Rect(),options);

    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;

All of this of course surrounded by try/catch.当然,所有这些都被 try/catch 包围。

After playing around with this for a while, I think the issue is probably that the image file really isn't there.在玩了一段时间之后,我认为问题可能是图像文件确实不存在。

If you are attempting to access a local file on your computer, that file won't be in the Android sandbox where the app is actually running.如果您尝试访问计算机上的本地文件,该文件将不会位于应用实际运行的 Android 沙箱中。 You'll need to add it to the app as a resource, then access it as a resource instead of a file.您需要将它作为资源添加到应用程序中,然后作为资源而不是文件访问它。

If you are attempting to access a web image, you need to download it first.如果您尝试访问网络图像,则需要先下载它。 The AbsolutePath of a web image ' http://example.com/example.jpg ' would be '/example.jpg', which won't do you much good when you try to open it. Web 图像“ http://example.com/example.jpg ”的 AbsolutePath 将是“/example.jpg”,当您尝试打开它时,这对您没有多大好处。

Use decodeStream instead of decodeFile .使用decodeStream而不是decodeFile

Open the stream with getContentResolver().openInputStream(myUri) .使用getContentResolver().openInputStream(myUri)打开流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM