Robert Bana
Robert Bana

Reputation: 2195

"Permission denied" when trying to handle a "Share Image" intent from Browser in my own activity

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)) 
                    ...

Upvotes: 3

Views: 1877

Answers (2)

peetasan
peetasan

Reputation: 857

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:

content://htcbrowser/share_image/data/data/com.android.browser/app_sharedimage/logo3w.png?image/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();
            }
        }
    }

Upvotes: 0

nandeesh
nandeesh

Reputation: 24820

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.

Upvotes: 1

Related Questions