21

I have a button, and I want when I click on it the image gets saved into the sd card ( or the internal storage, as in htc one x we don't have an external storage like an sd card )

this is my code:

            sd.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                MpClick.start();
                File myDir=new File("/sdcard/Saved_images");
                myDir.mkdirs();
                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "Image-"+ n +".jpg";
                File file = new File (myDir, fname);
                if (file.exists ()) file.delete (); 
                try {
                       FileOutputStream out = new FileOutputStream(file);
                       bitMapToShare.compress(Bitmap.CompressFormat.JPEG, 600, out);
                       out.flush();
                       out.close();

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

and how do I make a message appears in it it's written "Your image was saved." like an alert but for 2seconds and then disappears

John Jared
  • 790
  • 3
  • 16
  • 40

2 Answers2

93

try this

private void SaveImage(Bitmap finalBitmap) {

   String root = Environment.getExternalStorageDirectory().toString();
   File myDir = new File(root + "/saved_images");    
   myDir.mkdirs();
   Random generator = new Random();
   int n = 10000;
   n = generator.nextInt(n);
   String fname = "Image-"+ n +".jpg";
   File file = new File (myDir, fname);
   if (file.exists ()) file.delete (); 
   try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

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

and add this in manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Look at this answer Android saving file to external storage

EDIT : By using this line you can able to see saved images in the gallery view.

sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
        Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Community
  • 1
  • 1
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • It's working perfectly, only one problem that the album "saved_images" doesn't appear in the gallery but using a file manager I can see it made a folder "saved_images" and inside it the image I saved .. how do I make it, save it in the gallery – John Jared Aug 07 '12 at 16:41
  • @AhmedAl-ekrii you might be testing this on emulator, and emulator update slowly. Use media trigger before entering the gallery. You'll find the specified folder there. – Numair Aug 07 '12 at 18:28
  • no, I'm using my phone HTC One X .. what do u mean by using a media trigger ? ( I'm a starter, sorry ) – John Jared Aug 07 '12 at 19:55
  • 4
    It's ok, the problem is the gallery cache for anyone that faces my problem, use this code, it'll work sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); – John Jared Aug 07 '12 at 21:42
  • 4
    It's probably better to use a date stamp instead of random numbers, to avoid duplicates. – Oleksiy Oct 03 '14 at 20:12
2

Use Toast message

like

Toast.makeText(Your_class_name.this,
                    "Your image is saved to this folder", Toast.LENGTH_LONG)
                    .show();
Numair
  • 1,062
  • 1
  • 20
  • 41