“许可被拒绝”当尝试处理“共享图像”时我自己的活动中来自浏览器的意图
我正在尝试编写一个简单的活动,该活动执行以下操作: 用户按住在 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(权限被拒绝”)
我如何打开此图像文件?是否需要在清单中添加一些权限?
这是相关的代码:
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))
...
I'm trying to write a simple activity which does the following:
A user holds down on an image viewed in the Android Browser and then presses "Share image" and chooses my Activity to handle it. I want to be able to open the image file from within my activity.
The file path ends up being: /data/data/com.android.browser/app_sharedimage/SOME_FILE_NAME.jpg
and calling "File()" on the path results in a permission error:
java.io.FileNotFoundException: /data/data/com.android.browser/app_sharedimage/SOME_FILE_NAME.jpg (Permission denied")
How can I open this image file? Is there some permission I need to put in the manifest?
Here's the relevant code:
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))
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,除非您的手机已root,否则您将无法访问任何其他应用程序的私人文件。所以我认为你将无法打开图像。
AFAIK you wont be able to access private files of any other applications unless your phone is rooted. So I think you wont be able open the image.
我可以在 HTC Desire 上的浏览器中显示(我想这就是您打开的意思)图像,并且它没有 root。
我像您一样使用 Intent 中的 extras 中的 URI。然后我获取一个 InputStream 并从中创建一个 Drawable,它可以显示在 ImageView 中。
我得到的URI是:
I can display (I suppose this is what you mean by opening) an image from the browser on a HTC Desire and it is not rooted.
I use the URI just like you from the extras in the Intent. Then I get an InputStream and create a Drawable from it, which can be displayed in an ImageView.
The URI I get is: