我正在尝试编写一个执行以下操作的简单活动: 用户按住Android浏览器中查看的图像,然后按“共享图像”并选择我的活动来处理它。我希望能够从我的活动中打开图像文件。
文件路径最终为:/data/data/com.android.browser/app_sharedimage/SOME_FILE_NAME.jpg 并在路径上调用“File()”会导致权限错误: java.io.FileNotFoundException:/data/data/com.android.browser/app_sharedimage/SOME_FILE_NAME.jpg(Permission denied“)
如何打开此图像文件?我需要在清单中加入一些许可吗?
以下是相关代码:
if (Intent.ACTION_SEND.equals(intent.getAction())) {
if (extras.containsKey(Intent.EXTRA_STREAM)) {
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
String scheme = uri.getScheme();
if (scheme.equals("content")) {
String mimeType = intent.getType();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null,
null, null);
cursor.moveToFirst();
String filePath = cursor.getString(cursor
.getColumnIndexOrThrow(Images.Media.DATA));
...
...
new FileBody(new File(filePath))
...
答案 0 :(得分:1)
AFAIK除非您的手机已植根,否则您将无法访问任何其他应用程序的私人文件。所以我认为你无法打开图像。
答案 1 :(得分:0)
我可以通过HTC Desire上的浏览器显示(我想这是你打开的意思)并且它没有扎根。
我使用与你在Intent中的额外内容中的URI一样。然后我得到一个InputStream并从中创建一个Drawable,它可以在ImageView中显示。
我获得的URI是:
含量://htcbrowser/share_image/data/data/com.android.browser/app_sharedimage/logo3w.png图像/ PNG
if (intent.getType() != null && intent.getType().indexOf("image/") != -1) {
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_STREAM)) {
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
try {
InputStream is = getContentResolver().openInputStream(uri);
Drawable myDrawable = Drawable.createFromStream(is, "srcName");
iv.setImageDrawable(myDrawable);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}