1

I have numerous images in my resource folder of my android application. I would like to copy the files from the resource folder to storage and maintain their original image type (bitmap, jpg, png, etc) I would also like to have the original file name.

I am attempting to do something along the lines of...

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...   
        }
}
Matthew
  • 3,886
  • 7
  • 47
  • 84
  • Did you check the answer to this question http://stackoverflow.com/questions/4509877/android-dev-help-saving-an-image-from-res-raw-or-asset-folder-to-the-sd-card – triggs Apr 02 '13 at 15:54
  • You don't need to actually instance the bitmap to copy it. So I'll just redirect you to http://stackoverflow.com/questions/9292954/how-to-make-a-copy-of-a-file-in-android – DigCamara Apr 02 '13 at 15:54
  • Sorry to pile on additional question links. But I think the answer to this: http://stackoverflow.com/questions/649154/save-bitmap-to-location is going to be the easiest for you to drop into the code you've already got to get it working. – FoamyGuy Apr 02 '13 at 16:09
  • @DigCamara That question shows how to copy from a File object, but no where does it show how to get a File object from a drawable resource which is what you'd need to do in order to use that. – FoamyGuy Apr 02 '13 at 16:11
  • @FoamyGuy I guess the best linked answer would be the one triggs submitted. Still, I don't see much harm in leaving the link to the answer I found. – DigCamara Apr 02 '13 at 16:25
  • @DigCamara definitely no harm done =) – FoamyGuy Apr 02 '13 at 17:22

1 Answers1

0

To get the filename, I would use an extra paramter, the original path. Than you can loop over all the files in the original directory, to get the filenames. If you have the filenames, you can either one of the links mentioned in the comments...

This would be your final solution (UNTESTED):

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

    }
}

Be sure to also close the FileOutputStream, and do some extra checks to see the the File[] files is actually holding files and not directories.

NOTE: If you really work with the resouces folder, you can use something like this as well:

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

To loop over the resources

Entreco
  • 12,738
  • 8
  • 75
  • 95
  • Ah! I like that! So I will make every file a .PNG in this case, no matter what it is in the original resource folder. That works for me! I'll let you know if the code works, but looks great! – Matthew Apr 02 '13 at 22:35