android - using resources drawable in content provider

Posted by Russ Wheeler on Stack Overflow See other posts from Stack Overflow or by Russ Wheeler
Published on 2012-11-20T00:48:30Z Indexed on 2012/11/20 11:02 UTC
Read the original article Hit count: 482

I am trying to pass back an image through a content provider in a separate app. I have two apps, one with the activity in (app a), the other with content provider (app b)

I have app a reading an image off my SD card via app b using the following code.

App a:

public void but_update(View view) { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.jash.cp_source_two.provider/note/1"); InputStream inStream = null;

    try
    {
        inStream = resolver.openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(inStream);
        image = (ImageView) findViewById(R.id.imageView1);
        image.setImageBitmap(bitmap);
    }
    catch(FileNotFoundException e)
    {
        Toast.makeText(getBaseContext(), "error = "+e, Toast.LENGTH_LONG).show();   
    }

    finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException e) {
                Log.e("test", "could not close stream", e);
            }
        }
    }
};

App b: @Override public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
try { File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"pic2.png"); return ParcelFileDescriptor.open(path,ParcelFileDescriptor.MODE_READ_ONLY); } catch (FileNotFoundException e) { Log.i("r", "File not found"); throw new FileNotFoundException(); } }

In app a I am able to display an image from app a's resources folder, using setImageURi and constructing a URI using the following code.

int id = R.drawable.a2;
Resources resources = getBaseContext().getResources(); 
Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 
resources.getResourcePackageName(id) + '/' + 
resources.getResourceTypeName(id) + '/' + 
resources.getResourceEntryName(id) );
image = (ImageView) findViewById(R.id.imageView1);
image.setImageURI(uri);

However, if I try to do the same in app b (read from app b's resources folder rather than the image on the SD card) it doesn't work, saying it can't find the file, even though I am creating the path of the file from the resource, so it is definitely there.

Any ideas? Does it restrict sending resources over the content provider somehow?

P.S. I also got an error when I tried to create the file with

File path = new File(uri); saying 'there is no applicable constructor to '(android.net.Uri)' though http://developer.android.com/reference/java/io/File.html#File(java.net.URI) Seems to think it's possible...unless java.net.URI is different to android.net.URI, in which case can I convert them?

Thanks

Russ

© Stack Overflow or respective owner

Related posts about android

Related posts about android-contentprovider