使用原始名称和类型将图像资源从资源复制到android中的存储器

使用原始名称和类型将图像资源从资源复制到android中的存储器,android,Android,我的android应用程序的资源文件夹中有许多图像。我想将文件从资源文件夹复制到存储器,并保持其原始图像类型位图、jpg、png等。我还想拥有原始文件名 我正试图做一些与…相关的事情 public static void CopyImagesFromResourceToStorage(Context c,int[] resources,String path) { File dir = new File(path); for(int i=0;i<r

我的android应用程序的资源文件夹中有许多图像。我想将文件从资源文件夹复制到存储器,并保持其原始图像类型位图、jpg、png等。我还想拥有原始文件名

我正试图做一些与…相关的事情

public static void CopyImagesFromResourceToStorage(Context c,int[] resources,String path)
    {
        File dir = new File(path);

        for(int i=0;i<resources.length;i++)
        {
            File file = new File(path+"???Filename???");
            Bitmap bm = BitmapFactory.decodeResource(c.getResources(), resources[i]);

            //Copy Bitmap to the file here...   
        }
}

要获取文件名,我将使用一个额外的参数,即原始路径。然后,您可以循环原始目录中的所有文件,以获取文件名。如果你有文件名,你可以选择评论中提到的任何一个链接

这将是您未经测试的最终解决方案:

public static void CopyImagesFromResourceToStorage(Context c,int[] resources,String path, String originalpath)
{
    // Get the directory of your original files
    File dir = new File(originalpath);

    // List all files in that directory
    File[] files = dir.listFiles();
    // Loop through all the files in your original directory
    for(File original: files)
    {
        // Create a filename: <target_directory> + original.getName();
        String filename = path + "\\" + original.getName();
        Bitmap bm = BitmapFactory.decodeResource(c.getResources(), resources[i]);
        try {
           FileOutputStream out = new FileOutputStream(filename);
           bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        } catch (Exception e) {
           e.printStackTrace();
        }

    }
}

要循环浏览资源

您是否检查了此问题的答案?您不需要实际引用位图来复制它。因此,我将把你重定向到抱歉,以堆积更多的问题链接。但我认为答案是:对你来说,将是最容易进入你已经拥有的代码,让它工作的地方。@DigCamara这个问题展示了如何从文件对象复制,但它没有说明如何从可绘制资源中获取文件对象,而这正是使用该资源所需的。@FoamyGuy我想最好的链接答案应该是triggs提交的答案。不过,我不认为留下我找到的答案的链接有什么害处。啊!我喜欢!因此,在这种情况下,我将使每个文件都成为.PNG,无论它在原始资源文件夹中是什么。那对我有用!我会让你知道代码是否有效,但看起来很棒!
Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}