0

I want my local image file (located at res/drawable) to be converted to a Uri, so I can put it into a Intent to start an Activity.

This is what I got until now:

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setType("image/*");

    Uri uri = Uri.fromFile(new File("/res/drawable/photo.jpg"));
    sendIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.share)));

Then, the activity that receives this intent, displays the image in a ImageView. Something like this:

    Uri imageUri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    ImageView myImage = (ImageView) findViewById(R.id.imageView_receive);
    myImage.setImageBitmap(bitmap); 

But instead of displaying the image, it throws an java.io.FileNotFoundException

java.io.FileNotFoundException: No resource found for: android.resource://com.my.package/drawable/photo.jpg

I searched and tried other ways to get a Uri through my file:

    Uri uri = Uri.parse("file:///res/drawable/photo.jpg");
    Uri uri = Uri.parse("android.resource://com.my.package/drawable/photo.jpg"); 

But all of them returned the same error for me.

Why is my image not being recognized?

Rikkin
  • 509
  • 1
  • 5
  • 20

2 Answers2

1

You should create ContentProvider for your application, which will allow you to share resources.

Try this lib, it works awesome. https://github.com/commonsguy/cwac-provider

Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61
1

I want my local image file (located at res/drawable) to be converted to a Uri, so I can put it into a Intent to start an Activity.

Resources are files on your developer machine. They are not files on the device.

This is what I got until now

That will not work, as AFAIK there are zero Android devices with a /res directory off of the filesystem root, and your drawable resources would not be there anyway.

Why is my image not being recognized?

Your third one (android.resource scheme) is the closest. Get rid of the .jpg extension.

However, many third-party apps will not recognize that scheme. And, if you are implementing the activity that is to handle this request, I have no idea why you are using ACTION_SEND. If your objective is to only work within your own app, skip the Uri, package the drawable resource ID (R.drawable.photo) as an int extra, and have your other activity use that resource ID more naturally (e.g., setImageResource() on the ImageView).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the tips. I got rid of the extension .jpg and it found my image! I implemented the activity that handle it just to practice concepts that I am studying right now. Anyway, that really helped. – Rikkin Jun 07 '17 at 16:05