fah*_*dik 5 java android android-fileprovider
我正在尝试通过instagram意图共享文件,但我意识到在API 24 ++上,我不能在未经许可的情况下仅将URI共享给其他应用。
按照此https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/zh-CN教程,我已经设置了提供程序并提供了这样的路径:
表现
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="myapplication.file_provider"
android:exported="false"
android:grantUriPermissions="true"
tools:node="replace">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Run Code Online (Sandbox Code Playgroud)
注意:有`tools:node = replace“来覆盖库RxPaparazzo中的提供者。
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="files/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)
保存位图并获取uri的代码
public static Uri savePicture(Context context, Bitmap bitmap) {
int cropHeight;
if (bitmap.getHeight() > bitmap.getWidth()) cropHeight = bitmap.getWidth();
else cropHeight = bitmap.getHeight();
bitmap = ThumbnailUtils.extractThumbnail(bitmap, cropHeight, cropHeight, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
context.getString(R.string.app_name)
);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile = new File(
mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"
);
// Saving the bitmap
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
FileOutputStream stream = new FileOutputStream(mediaFile);
stream.write(out.toByteArray());
stream.close();
} catch (IOException exception) {
exception.printStackTrace();
}
// Mediascanner need to scan for the image saved
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(mediaFile);
mediaScannerIntent.setData(fileContentUri);
context.sendBroadcast(mediaScannerIntent);
return fileContentUri;
}
Run Code Online (Sandbox Code Playgroud)
撰写文件提供者Uri
Uri savedBitmap = ImageUtility.savePicture(getContext(), shareBitmap);
File media = new File(savedBitmap.getPath());
Uri contentUri = FileProvider.getUriForFile(getContext(), "myapplication.file_provider", media);
Run Code Online (Sandbox Code Playgroud)
记录错误:
java.lang.IllegalArgumentException:无法找到包含/storage/emulated/0/Pictures/Polfaced/IMG_20170203_155130.jpg的已配置根目录
使用commonware的StreamProvider试过,但我认为图书馆有不同的目的(固定另一种类型的问题),试图保存文件,getFileDirs而不是Environment.getExternalStoragePublicDirectory但图像不会被保存,让similiar错误/
更改
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="files/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)
至
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)
如您在上面共享的链接中所述。
以下提供者:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="files/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)
将解决看起来像的路径
/storage/emulated/0/files/
Run Code Online (Sandbox Code Playgroud)
但是您共享的路径
/storage/emulated/0/Pictures/Polfaced/IMG_20170203_155130.jpg
Run Code Online (Sandbox Code Playgroud)
不包含以下内容或以以下内容开头:
/storage/emulated/0/files/
Run Code Online (Sandbox Code Playgroud)
要了解什么
path="."
Run Code Online (Sandbox Code Playgroud)
确实,请仔细阅读您共享的链接。
| 归档时间: |
|
| 查看次数: |
2672 次 |
| 最近记录: |