【发布时间】:2016-08-03 11:52:25
【问题描述】:
我正在尝试传递驻留在我的应用程序的 res/raw 目录中的图像以及共享意图。
我遵循FileProvider docs 中描述的过程,这是我的代码:
AndroidManifest.xml
<application ...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>
</application>
res/xml/paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="shared" path="./"/>
</paths>
我活动中的代码:
String shareToPackage = ...
File imageFile = new File(context.getFilesDir().getPath() + "/image");
if (!imageFile.exists()) { // image isn't in the files dir, copy from the res/raw
final InputStream inputStream = context.getResources().openRawResource(R.raw.my_image);
final FileOutputStream outputStream = context.openFileOutput("image", Context.MODE_PRIVATE);
byte buf[] = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
imageFile = new File(context.getFilesDir().getPath() + "/image");
}
if (!imageFile.exists()) {
throw new IOException("couldn't find file");
}
final Uri uri = Uri.fromFile(imageFile);
context.grantUriPermission(shareToPackage, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_TEXT, "here's the image");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setPackage(shareToPackage);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
由于无法访问我在其他应用中获取的文件,因此上述内容不起作用:
java.io.FileNotFoundException:FILE_PATH:打开失败:EACCES (权限被拒绝)
知道我在这里做错了什么吗?
谢谢。
【问题讨论】:
-
你有没有解决过这个问题 - 面对完全相同的......
-
@brandall 不。我放弃了它,因为浪费了太多时间。如果您确实找到了解决方案,请在此处回答,以便将来其他人也不需要浪费太多时间。祝你好运!
-
难以置信... 6 个小时的厕所已经在这上面了,因为事情应该如此简单。如果我解决它,我会回帖.....
标签: java android android-fileprovider