【问题标题】:Get absolute path of android drawable image获取android可绘制图像的绝对路径
【发布时间】:2014-11-03 16:10:19
【问题描述】:

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

可绘制如下

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

【问题讨论】:

标签: android path drawable


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    Uri path = Uri.parse("android.resource://com.nikhil.material/" + R.drawable.image);
    

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

    Uri path = Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.image);
    

    【讨论】:

    • 也许可以使用BuildConfig.APPLICATION_ID like Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.image); idea from -> stackoverflow.com/a/29109743/1815624
    【解决方案3】:

    可绘制资源在运行时添加到二进制文件中,因此无法通过路径访问。 你可以解码并使用drawables。

    如果您需要可以在运行时通过路径访问的资源,请尝试使用 assets 文件夹。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      相关资源
      最近更新 更多