获取android可绘制图像的绝对路径 [英] Get absolute path of android drawable image

查看:23
本文介绍了获取android可绘制图像的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 android 应用程序中获取我的可绘制图像的绝对路径或 File 对象.有没有办法做到这一点?

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 如下

Drawable is as following

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

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

推荐答案

如果您的应用安装在 sdcard 上,请尝试以下代码:

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);
}

这篇关于获取android可绘制图像的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆