Numbering of image saved from app resource to SD card

只愿长相守 提交于 2019-12-02 05:19:26

From your question and comments i can understand that you want to save n number of images to SDCard.

To save follow the steps

STEP 1: Get All the Images you need. Make sure you getting the list of images correctly here.

STEP 2: Count number of images in the list and store it in variale

      int numberOfImages = 15;// Get it dynamically 

STEP 3: Now loop it to store all the images in sequential order

   //Create Directory to store images in SDCard
   String root = Environment.getExternalStorageDirectory().toString();
       File myDir = new File(root + "/saved_images");
       if(!myDir.exists()){
           myDir.mkdirs();
          } 
           // You have to get next image here from the resource here
           bm = BitmapFactory.decodeResource( mContext.getResources(), images[i]);// value for itemPos should be given here.

           // Get Last Saved Number
           SharedPreferences savedNumber = getSharedPreferences(PREFS_NAME, 0);
           int lastSavedNumber = savedNumber.getInt("lastsavednumber",0); 
           lastSavedNumber++;
           String fname = "Image-"+lastSavedNumber+".png";

           File file = new File (myDir, fname);
           if (file.exists ()) {file.delete (); }
           try {
                  FileOutputStream out = new FileOutputStream(file);
                  bm.compress(Bitmap.CompressFormat.JPEG, 90, out);//Your Bitmap from the resouce
                  out.flush();
                  out.close();

               } catch (Exception e) {
                  e.printStackTrace();
               }

      //To Store the last Number
     SharedPreferences saveNumber = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
     SharedPreferences.Editor editorset = saveNumber.edit();
     editorset.putInt("lastsavednumber",lastSavedNumber);   
     editorset.commit();   

The duplication can take place if you do any thing wrong in your first step.

EDIT To Store All Images in Sequential Order Use SharedPreferences to Store last saved image number.

    public static final String PREFS_NAME = "ImageNumber";  

    // Get Last Saved Number
    SharedPreferences savedNumber = getSharedPreferences(PREFS_NAME, 0);
    int lastSavedNumber = savedNumber.getInt("lastsavednumber",0); 
    lastSavedNumber++;
    String fname = "Image-"+lastSavedNumber+".png";

    //To Store Last Saved Number
     SharedPreferences saveNumber = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
     SharedPreferences.Editor editorset = saveNumber.edit();
     editorset.putInt("lastsavednumber",lastSavedNumber);   
     editorset.commit();   

Simply use for loop. if you getting size how much images you want save on SD card then,

for(int n=1 ; n <= size ; n++){
String fname = "Image-"+ n +".png";
 // you other stuff here
}

Hope this helps you.

Forgive Random, if you want images in sequential order (as Pragnani suggested and you approved on comments above) and supposing that your code is ok, do this:

    Override
    public void onClick(View arg0) {
        String root = Environment.getExternalStorageDirectory().toString();
        File imagesFolder = new File(root + "/imagesFolder");    
        imagesFolder.mkdirs();

        for (int i = 0; i < 10; i++) { 
            String fname = "Image-" + i + ".png";
            File file = new File (imagesFolder, fname);

            if (file.exists ()) file.delete (); 
            try {
               FileOutputStream out = new FileOutputStream(file);

               bm.compress(Bitmap.CompressFormat.PNG, 100, out);
               out.flush();
               out.close();
               Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
               e.printStackTrace();
               Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }

Test it and let me know.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!