1

I'm now developing an app using android studio. Now I want to use an image file which is stored at my desktop. Then I also need its uri to do something. Now I put it in drawable file, but I don't know how to call it as a file and get its url. I think I should implement something like this:

File file = new File(?????);
Uri uri = Uri.fromfile(file);

What should I put inside the clause? I've tried "res:///"+R.drawable.image, but it doesn't work. Can someone help me?

hsw8906
  • 31
  • 9

4 Answers4

0

If you want to use your image as Bitmap (as far as i understand from your question) Use this for getting bitmap from drawable folder.

Bitmap image= BitmapFactory.decodeResource(context.getResources(), R.drawable.image_name);

Then you can save this bitmap to the telephone for uri but i think you are trying to access bitmap of it.

Alperen Kantarcı
  • 1,038
  • 9
  • 27
0

To create a file from uri, you also need to provide the file path

Add the following method to your activity outside the onCreate method

private String getPath(Uri contentUri) {
    String[] proj = {MediaStore.Images.Media.DATA};
    CursorLoader loader = new CursorLoader(getContext(), contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String result = cursor.getString(column_index);
    cursor.close();
    return result;
}

and then create file like this

   String filePath =  getPath(put your uri here);
   File file = new File(filePath);

Hope it works for you

Ibtihaj Uddin
  • 138
  • 1
  • 8
0

you can get Uri, you will replace pic1 with your image name

Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/drawable/pic1"); img.setImageURI(uri);

0

try to use ContentResolver to open resource URIs:

Uri uri = Uri.parse("android.resource://your.package.name/drawable/image_filename");

//To open file

InputStream stream = getContentResolver().openInputStream(uri);