2

I have some images to be displayed in the Application (When user selects an image from it, like picking from gallery ).

The question is how to copy the images I am putting in the assets folder in the code to a folder on the SD card.

Abhijat Biswas
  • 174
  • 1
  • 14
  • i tried image to push direct sdcard, but it not work.Then use this http://stackoverflow.com/questions/12173468/copy-images-from-assest-to-sd-card-on-install-android-application –  Apr 11 '13 at 08:01

3 Answers3

3
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open(fileName);

Get the AssetManager and call open with the filename you want to copy as parameter. Then you can simply copy in this way

File out = new File(Environment.getExternalStorageDirectory(), fileName);
byte[] buffer = new byte[BUFFER_LEN];
FileOutputStream fos = new FileOutputStream(out);
int read = 0;

 while ((read = is.read(buffer, 0, BUFFER_LEN)) >= 0) {
        fos.write(buffer, 0, read);
  }

fos.flush();
fos.close()
is.close()

remember to add the WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml file

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • thank you for answer,this will be use for specified file but in my project there are images(not a single image). –  Apr 11 '13 at 08:30
  • It looks like Environment.getExternalStorageDirectory is a method, not public member, so it should be Environment.getExternalStorageDirectory() (with braces) – Grimmy Feb 13 '14 at 14:54
  • yes of course. It was a typo. Thanks for pointing it out @Grimmy – Blackbelt Feb 13 '14 at 14:55
2
private void copyFileAssets() {
        AssetManager assetManager = getAssets();            
        InputStream in = null;
        OutputStream out = null;
        try {
          in = assetManager.open("ceo.jpg");
          out = new FileOutputStream(Environment.getExternalStorageDirectory()+File.separator+ "abc.jpg");
          copyFile(in, out);
          in.close();
          in = null;
          out.flush();
          out.close();
          out = null;
        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }       

}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}

you have to add this permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0
  • Get the bitmap resource using one of the many ways.

Ex:

Bitmap bitmap = BitmapFactory.decodeResource(resource, id); // get your bitmap that you want to save on SD card.
  • Get the path to store the resource.

String compressedFilePath = Environment.getExternalStorageDirectory() + File.separator + "FOLDERNAME" + File.separator + "FILENAME.jpg";

  • Save using compress method. (Shortcut way to save image as per docs)

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(pathToStore))); // use 100 to make sure image quality is not reduced(Read the docs)

Note: You need Write(and may be read in future) access to write to SDCARD.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
MichaelS
  • 7,023
  • 10
  • 51
  • 75
  • so basically you are taking a compressed image, decompressing it, then recompressing it back on the sdcard. isn't it simpler to, like, copy it ? – njzk2 Apr 11 '13 at 09:54
  • Not quite. Take the image, call the compress method that in turn just returns the same image if compression value is set to 100. Its a simple way to save an image to any location. No compression nor decompression. – Bharath Lakshman Apr 11 '13 at 10:18
  • yes, there is. a jpeg is compressed, even if the compression is lossless. you are creating a bitmap from a jpeg (or whatever), hence decompressing it, then creating a new jpeg file, which is a compression. – njzk2 Apr 11 '13 at 11:40