[英]FileNotFoundException: /storage/emulated/0/Download/ - Android Oreo
I have an app. 我有一个应用。 There is an imageview. 有一个imageview。 Image loads from Url and also there is a button for sharing image. 从网址加载图片,还有一个共享图片的按钮。 when i press share button i got below error. 当我按下共享按钮时,出现以下错误。
java.io.FileNotFoundException: /storage/emulated/0/Download/share_image_1536343274460.png (Permission denied) java.io.FileNotFoundException:/storage/emulated/0/Download/share_image_1536343274460.png(权限被拒绝)
my manifest xml: 我的清单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 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>
This is my java code 这是我的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;
}
You need to use FileProvider.getUriForFile(context, "provider authority from manifest", file);
您需要使用FileProvider.getUriForFile(context, "provider authority from manifest", file);
instead of Uri.fromFile(file)
when targeting above API level 24 and above. 定位到API级别24或更高版本时,而不是Uri.fromFile(file)
。
Your main change will be in method getLocalBitmapUri()
@ this line: 您的主要更改将在方法getLocalBitmapUri()
@此行中:
bmpUri = Uri.fromFile(file);
So change like 所以改变像
If (above API 24 ) then use 如果(高于API 24),则使用
FileProvider.getUriForFile(context, "provider authority from manifest", file);
else old approach. 否则旧的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.