【问题标题】:Get path of a drawable image so that I can post it to the backend获取可绘制图像的路径,以便我可以将其发布到后端
【发布时间】:2026-02-09 05:45:01
【问题描述】:

我有一组图标存储在可绘制文件夹中。

根据用户的选择,我会将这些图标发布到后端。

当我尝试检索这些可绘制对象的路径时,出现以下错误

HTTP 失败:java.io.FileNotFoundException:(没有这样的文件或目录)

这就是我获取路径的方式

public String getURLForResource (int resourceId) {
        return Uri.parse("android.resource://"+ BuildConfig.APPLICATION_ID+"/" +resourceId).toString();
    }

File fileToUpload  = new File(getURLForResource(R.drawable.icon));

我提到了以下link 表示无法检索drawable 的路径。

知道如何解决这个问题吗?

【问题讨论】:

标签: android file android-studio android-drawable android-resources


【解决方案1】:

作为解决方案,您可以将可绘制对象写入文件。

将drawable转换为位图

Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.your_drawable);

将位图保存到文件

String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, "image" + System.currentTimeMillis() + ".jpg");
try (FileOutputStream out = new FileOutputStream(file)) {
    bm.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.flush(); // Not really required
    out.close(); // do not forget to close the stream
} catch (IOException e) {
    e.printStackTrace();
}

你现在可以使用文件了。

【讨论】:

    最近更新 更多