1

I have 10 images and want to save all this images in android application memory . so whenever any use install this apps , he have already this all images .

i have hard coded many times and not get good response .

is there any way . so i will done it.

please help me

Rahul Rawat
  • 999
  • 2
  • 17
  • 40

2 Answers2

1

Store all your images in the assets folder of your APK. Once it gets installed, scan the internal memory to see if desired images are there. If not, copy them there. In this way, even when users clear data of your application, you can copy them back. Another good thing is the user will not know anything about it as well, so it a good user-experience. Only thing would be your APK size would increase, so manage accordingly.

Try the below piece of code

InputStream inputStream = getAssets().open("yourfile.jpg");
            OutputStream out = new FileOutputStream(new File("/sdcard/yourfile.jpg"));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            inputStream.close();
            out.flush();
            out.close();
Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
  • i am using it. http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard of first answer but it give me exception "failed to copy asses file : images" . do i need to change here files = assetManager.list(""); .. i simply copy all the files in ASSESTS FOLDER – Rahul Rawat Oct 30 '12 at 11:20
  • You have to simply copy the images, then using File Streams copy it out. AssetManager cannot differentiate between different files, for it, everything is a stream of data. So try to follow the same – Royston Pinto Oct 30 '12 at 13:22
0

You can keep the files in your asset or res folder , here I have kept the file in res/drawable/ and copy them on sdcard when I require. In below code first we check if file doesn't exist the we create a bitmap from the drawable and write the file out to sdcard.

  File file = new File(pathExt+"/Pictures/", "s1.png");
                if(isSDCARDMounted()){
                if (!file.exists()) {
                    bmp = BitmapFactory.decodeResource(getResources(),
                            R.drawable.s1);

                    try {

                        FileOutputStream outStream = new FileOutputStream(file);
                        bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                        outStream.flush();
                        outStream.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                        }
}

isSDCARDMounted : function for checking if card is mounted or not

pathExt : variable for external storage directory path

Make sure have permission set for writing on external storage

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Sunny Kumar Aditya
  • 2,806
  • 4
  • 26
  • 38