1

Is there a way on how can I transfer Images from res/drawable folder to a folder in sdcard? And still get the quality of the picture and the details of it?

Stella
  • 3,923
  • 4
  • 15
  • 15
  • Why would you want to do this? – Raghav Sood Sep 19 '12 at 16:51
  • Because I want that when my app started.,It'll just create a 'folder' in sdcard and then load the pictures from drawable to that 'folder'. – Stella Sep 19 '12 at 16:53
  • 1
    Why not load it from drawable itself? Why duplicate data and consume extra space? – Raghav Sood Sep 19 '12 at 16:54
  • Because the idea is to load images from sdcard, not in drawable. I wanted to do this because in time if someone tested the app, he/she will not create a folder manually in sdcard. Once the app is tested it has no errors. – Stella Sep 19 '12 at 16:59
  • That's fine, but why do you need to load it from the SD Card, when you already have it in your app? You needlessly duplicate data. If you need the image on the SD card, then you should not bundle it with your app. Instead, you should have the user download it on first run, as that will save the user space. – Raghav Sood Sep 19 '12 at 17:03
  • It's just a sample, I know what you are telling me., But the point is that for example the app is downloaded, the app is like on the go. And the fact that also I wanted to learn that. if it is possible. – Stella Sep 19 '12 at 17:09

1 Answers1

3

You'll have to use BitmapFactory.decodeResource(), by passing it the resource ID of your item, to first generate a Bitmap.

Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mybitmap);

Then, you can save it to the memory card using the answer in this thread.

Something like this:

try {
    FileOutputStream out = new FileOutputStream(filename);
    myBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
    e.printStackTrace();
}

NB: @RaghavSood is right in his comments; there really is no reason why you would need to do this. You can just load the drawable resource at runtime instead of consuming space on the memory card.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • 1
    Tried this one, but is there a way to transfer many image, not only one. – Stella Sep 19 '12 at 17:00
  • Make it a method, then call it more than once with a different resource ID...? There isn't a way to take a clump of drawables and convert them to `Bitmap`s automagically, no. – Cat Sep 19 '12 at 17:20