0

I am developing an app of images and images change with button cliks.When I save the image of ImageView then it saved to device and when I saved another image then the first one is deleted. So I remaind fail to save multiple images. Plz help. Here is main java.

 public class Main extends Activity {
 private ImageView hImageViewPic;
 private Button iButton, gButton; 
 Bitmap bbicon;

  private int currentImage = 0; 
  int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 };
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main); 

  hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
  iButton = (Button) findViewById(R.id.bNext); 
  gButton = (Button) findViewById(R.id.bPrev); 

  //Just set one Click listener for the image 

  iButton.setOnClickListener(iButtonChangeImageListener);
  gButton.setOnClickListener(gButtonChangeImageListener); 
    } 

  View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
  public void onClick(View v) {

  //Increase Counter to move to next Image

  currentImage++; 
  currentImage = currentImage % images.length;
   hImageViewPic.setImageResource(images[currentImage]);
   }
      };

  View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
 `public void onClick(View v) {

  //Increase Counter to move to next Image 

  currentImage--;
  currentImage = (currentImage + images.length) % images.length; 
  hImageViewPic.setImageResource(images[currentImage]);` 

   // here we set 3rd button to save the images

  Button bSaveImg = (Button) findViewById (R.id.bSave):
  ImageView imagePreview = (ImageView) findViewById (R.id.idImageViewPic);
  imagePreview.setImageResource (images [currentImage]);
  bSaveImg.setOnClickListener (new Button.OnClickListener () {

  @Override
    public void onClick (View arg0) {

  bbicon=BitmapFactory.decodeResource(getResources(),images [cirrentImage]);
  //ByteArrayOutputStream baosicon = new ByteArrayOutputStream(); 
  //bbicon.compress(Bitmap.CompressFormat.PNG,0, baosicon); 
  //bicon=baosicon.toByteArray();

   String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
   OutputStream outStream = null; 
   File file = new File(extStorageDirectory, "er.PNG");

   try { 
     outStream = new FileOutputStream(file); 
     bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
     outStream.flush(); 
     outStream.close(); 
    } catch(Exception e) {
       } 
     }
       });
    }
      };
    }

So plz help to eradicate this problem.

3004150
  • 9
  • 7
  • You need to change file name dynamically otherwise it will overwrite and you will not get multiple files. – keshav Dec 02 '13 at 11:39

3 Answers3

0

Change the name of the png file , like put the name of your png file with current time or date which is unique for every image .

Change

File file = new File(extStorageDirectory, "er.PNG");

to

File file = new File(extStorageDirectory, System.currentTimeMillis()+".PNG");
Smogger
  • 553
  • 5
  • 17
0

I prefer to save my images with DateTime as it acts as a Unique instance

String strFileName=android.text.format.DateFormat.format("dd-MM-yyyy-hh-mm-ssaa", new java.util.Date()).toString();
File file = new File(path, strFileName+ ".png");

Plus do remeber storing images on ExtStorageDirectory is not advisable. Rather than that make a Folder in External Storage and save it over there

File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/YourFolder/";
File dir = new File(path);
if (!dir.exists()) {
    if (dir.mkdirs()) {
    }
}

Edit For User

bSaveImg.setOnClickListener (new Button.OnClickListener () {

@Override
public void onClick (View arg0) {

bbicon=BitmapFactory.decodeResource(getResources(),images [cirrentImage]);
//ByteArrayOutputStream baosicon = new ByteArrayOutputStream(); 
//bbicon.compress(Bitmap.CompressFormat.PNG,0, baosicon); 
//bicon=baosicon.toByteArray();
///below code
File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/YourFolder/";
File dir = new File(path);
if (!dir.exists()) {
    if (dir.mkdirs()) {
    }
}
  OutputStream outStream = null; 
 ///below Code
String strFileName=android.text.format.DateFormat.format("dd-MM-yyyy-hh-mm-ssaa", new java.util.Date()).toString();
File file = new File(path, strFileName+ ".png");

 try { 
   outStream = new FileOutputStream(file); 
   bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
   outStream.flush(); 
   outStream.close(); 
  } catch(Exception e) {
   } 
   }
   });
  }
MDMalik
  • 3,951
  • 2
  • 25
  • 39
  • Thank you very much for so beautiful reply sir. But I am finding error. There is no catch (Exception e){ // so can you plz modify my java. Then I will understand it in a better way. Thanx – 3004150 Dec 02 '13 at 16:10
  • @3004150 can you please Edit your question and post the ERROR. – MDMalik Dec 03 '13 at 07:50
  • Sir I can not understand how can I put your code in java class. – 3004150 Dec 03 '13 at 08:40
0
 //Create Folder
 File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Images");
 folder.mkdirs();

 //Save the path as a string value
 String extStorageDirectory = folder.toString();
 File file = new File(extStorageDirectory, System.currentTimeMillis()+".PNG");

Dont forget to put try and catch and check if folder exists then do something else otherwise create the folder and put the below line in your manifest file.

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
Smogger
  • 553
  • 5
  • 17