【问题标题】:FileNotFoundException: /storage/emulated/0/Download/ - Android OreoFileNotFoundException: /storage/emulated/0/Download/ - Android Oreo
【发布时间】:2018-09-07 18:23:53
【问题描述】:

我有一个应用程序。有一个图像视图。图片从 Url 加载,还有一个用于共享图片的按钮。 当我按下分享按钮时,出现以下错误。

java.io.FileNotFoundException: /storage/emulated/0/Download/share_image_1536343274460.png(权限 拒绝)

我的清单 xml:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="50"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="50"/>

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.xxxx.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

provider_paths xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

这是我的java代码

btnShare.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();

        if (isReadStorageAllowed() == true) {
           onShareItem();
        }
        else
        {
            requestStoragePermission();
        }
    }
});

public void onShareItem() {
    // Get access to bitmap image from view
    imageView = (ImageView) findViewById(R.id.thumbnail);

    // Get access to the URI for the bitmap
    Uri bmpUri = getLocalBitmapUri(imageView);
    if (bmpUri != null) {
        //outfile is the path of the image stored in the gallery
        // Construct a ShareIntent with link to image
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_TEXT,marketLink);
        // Launch sharing dialog for image
        startActivity(Intent.createChooser(shareIntent, "Share Image"));
    } else {
         // ...sharing failed, handle error
    } 
}

public Uri getLocalBitmapUri(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file =  new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

【问题讨论】:

  • 这张图片在 SD 卡上吗?
  • @UmangBurman ,图片从 url 加载。
  • 好的,那么按钮的作用是什么?
  • System.currentTimeMillis() 每次都更改。我认为文件名不会每次都更改对吗?
  • @FaysalAhmed 这在奥利奥之前工作得很好。我的意思是,这适用于所有设备,但不适用于奥利奥。

标签: android filenotfoundexception sharing


【解决方案1】:

定位到 API 级别 24 及以上时,您需要使用 FileProvider.getUriForFile(context, "provider authority from manifest", file); 而不是 Uri.fromFile(file)

您的主要更改将在方法 getLocalBitmapUri() @ 这一行:

bmpUri = Uri.fromFile(file);

所以换个样子

如果(API 24 以上)则使用

FileProvider.getUriForFile(context, "provider authority from manifest", file);

其他旧方法。

【讨论】:

  • 我很抱歉,但这与 Permission Denied thingy 有什么关系?
  • 这是因为您从清单文件中获得了文件提供程序,因此该应用程序不会因FileUriExposed 异常而崩溃。但是您仍然使用 URI 类的旧 API 接收 URI。虽然它与 SDK 24 相比有所变化,因此我们需要使用 FileProvider 类来获取我们的 URI。
  • 哦,我明白了。所以,我希望这是解决方案。
  • 你可以自己试试。
  • 不,谢谢,我还有其他事情要做。 :)
猜你喜欢
  • 1970-01-01
  • 2020-11-03
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 2021-05-15
相关资源
最近更新 更多