1

I have app consist of days listview each day has its specific images placed in an infinite gallery class , what im trying to do is:

saving images with sequential number from app drawable resource ( infinite gallery class) to sd card,

im trying to get the sequential number of saved images as below :

first image :Image-1.png .

second image :Image-2.png .

third image : Image-3.png ,

and so on for all dayes .

with using :

 Random generator = new Random();

This will lead to :

first issue : saved images with random numbers .

second issue : its not saving all images was choose by user to save to sd card also it save some of image twice or three times .

This is the part of code which related to saving images :

 View vi=convertView; 
    final ViewHolder holder; 
    if(convertView==null){ 
        vi = inflater.inflate(R.layout.gallery_items, null); 
        holder=new ViewHolder(); 
        holder.text=(TextView)vi.findViewById(R.id.textView1); 
        holder.image=(ImageView)vi.findViewById(R.id.image); 
        holder.button=(Button)vi.findViewById(R.id.button_save);

 bm = BitmapFactory.decodeResource( mContext.getResources(), images[itemPos]);
         holder.image.setImageBitmap(bm);

        holder.button.setOnClickListener(new OnClickListener() {

  public void onClick(View arg0) {

      String root = Environment.getExternalStorageDirectory().toString();
        File imagesFolder = new File(root + "/Days pictures");    
        imagesFolder.mkdirs();

        Random generator = new Random();
        int n = 1000;
     n = generator.nextInt(n);
     String fname = "Image-"+ n +".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(mContext, "Saved", Toast.LENGTH_LONG).show();}   
        catch (Exception e) {
             e.printStackTrace();    
       Toast.makeText(mContext, e.toString(), Toast.LENGTH_SHORT).show();}}});

      vi.setTag(holder);}

    else holder=(ViewHolder)vi.getTag(); 
    holder.text.setText(name[itemPos]); 

    final int stub_id=images[itemPos]; 
    holder.image.setImageResource(stub_id); 

    return vi; } 

private ImageView getImageView() { 

    ImageView i = new ImageView(mContext); 

    return i; } }

thanks for your help .

androidqq6
  • 1,526
  • 2
  • 22
  • 47

3 Answers3

0

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.

Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
  • i replace this : Random generator = new Random(); int n = 10000; n = generator.nextInt(n); String fname = "Image-"+ n +".png"; with your code :for(int n=1 ; n <= size ; n++){ String fname = "Image-"+ n +".png"; but it doesnt save image to sd folder – androidqq6 Mar 28 '13 at 08:16
  • @androidqq6: put your code snippet of storing images on sd card from app to understand a solve your problem. – Amol Sawant Mar 28 '13 at 10:28
  • @androidqq6 : are you creating bitmap before saving an image like this, Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher); please refer below link for more details http://stackoverflow.com/questions/10558053/save-image-to-sdcard-from-drawble-resource-on-android – Amol Sawant Mar 28 '13 at 10:48
0

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.

JJ86
  • 5,055
  • 2
  • 35
  • 64
  • i tried this before , when press save button once atime , it save 10 images in the same time Sequentially as image-1,image-10, image-2,image-3 and so on – androidqq6 Apr 13 '13 at 11:40
  • my friend, my above code is part of full project,to be clear i will update the post with full class include pictures to be saved , it's better to understand – androidqq6 Apr 13 '13 at 12:51
0

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();   
vinothp
  • 9,939
  • 19
  • 61
  • 103
  • my dear friend im still new to development but what you mean by : // value for itemPos should be given here. in your answer , im try to solve my problem then i will update my post to check it , thanks – androidqq6 Apr 15 '13 at 11:34
  • I want to ask one question. From where you getting the image? and how you getting that image?. Can you update the code – vinothp Apr 15 '13 at 11:54
  • i will update now the whole activity in my post which include the for loop fo save images to be more clear my friend ,thanks – androidqq6 Apr 15 '13 at 11:57
  • Yes from the above code i can find that you will store only one image for number of times right. What you want to do now is `for example` if you want to store all the images in `Day1` then you need to get all the resources and assign to bitmap to store it in sdcard using loop – vinothp Apr 15 '13 at 12:37
  • to clear my self , imagine you are user exploring my application , you open Day1 gallery you like 2 images from Day1 and want to save it to sd , then opend Day2 you dont like any images from its gallery , then open Day3 you like 3 images in Day3 gallery and want to save it and so on , so you can finally liable to save any images you choose either 1 image or 9 images or all images one by one , this is my idea but i dont want to save whole day images in one step , this is how i want it to work , thanks – androidqq6 Apr 15 '13 at 12:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28246/discussion-between-vino-and-androidqq6) – vinothp Apr 15 '13 at 12:54
  • it save first imge oressed to save to sd card under name of Image-1.png , when you press second image to save it , it replace the first one , finally i have one image only in sd card folder – androidqq6 Apr 15 '13 at 19:55
  • yes it will save only one image. its a first step you have to do some other stuff as well – vinothp Apr 15 '13 at 21:22
  • edited the answer. It will store all images in sequential order. check it – vinothp Apr 16 '13 at 08:06
  • Hi my friend does i need to repeate this piece of code twice : //To Store the last Number SharedPreferences saveNumber = getApplicationContext().getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editorset = saveNumber.edit(); editorset.putInt("lastsavednumber",lastSavedNumber); editorset.commit(); – androidqq6 Apr 16 '13 at 11:55
  • no you can see the difference between two. In first one you getting the image number. In Second peace of code you storing the incremented number – vinothp Apr 16 '13 at 11:56
  • If you happy with my answer. then accept it as an answer. Happy coding. – vinothp Apr 17 '13 at 12:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28375/discussion-between-vino-and-androidqq6) – vinothp Apr 17 '13 at 13:19
  • would you please check this related new post to this question, thanks :http://stackoverflow.com/questions/16122799/save-clear-and-save-sharedpreferences-again – androidqq6 Apr 20 '13 at 16:52