Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get absolute path of android drawable image

I need to get the absolute path or the File object of my drawable image in android application. Is there any way to do this?

Drawable is as following

context.getResources().getDrawable(R.drawable.back_button)

like image 611
Gayan Dinuzhka Avatar asked Dec 05 '25 13:12

Gayan Dinuzhka


2 Answers

If you app is insalled on sdcard try this code :

Bitmap bitMap = BitmapFactory.decodeResource(getResources(),R.id.img1);

File mFile1 = Environment.getExternalStorageDirectory();

String fileName ="img1.jpg";

File mFile2 = new File(mFile1,fileName);
try {
       FileOutputStream outStream;

       outStream = new FileOutputStream(mFile2);

       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

       outStream.flush();

       outStream.close();

} catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
} catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
}

String sdPath = mFile1.getAbsolutePath().toString()+"/"+fileName;

Log.i("hiya", "Your IMAGE ABSOLUTE PATH:-"+sdPath); 

File temp=new File(sdPath);

if(!temp.exists()){
       Log.e("file","no image file at location :"+sdPath);
}
like image 191
KOTIOS Avatar answered Dec 07 '25 03:12

KOTIOS


Uri path = Uri.parse("android.resource://com.nikhil.material/" + R.drawable.image);

OR

Uri otherPath = Uri.parse("android.resource://com.nikhil.material/drawable/image");

OR

Uri path = Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.image);
like image 36
Nikhil Borad Avatar answered Dec 07 '25 02:12

Nikhil Borad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!