This is the code itself. He borrowed borrowedly from Habr.

ImageView imageView; // ImageView, содержащий изображение, которое нужно сохранить String folderToSave = Environment.getExternalStorageDirectory().toString(); // папка куда сохранять, в данном случае - корень SD-карты private String SavePicture(ImageView iv, String folderToSave) { OutputStream fOut = null; Time time = new Time(); time.setToNow(); try { File file = new File(folderToSave, Integer.toString(time.year) + Integer.toString(time.month) + Integer.toString(time.monthDay) + Integer.toString(time.hour) + Integer.toString(time.minute) + Integer.toString(time.second) +".jpg"); // создать уникальное имя для файла основываясь на дате сохранения fOut = new FileOutputStream(file); Bitmap bitmap = (BitmapDrawable) iv.getDrawable().getBitmap(); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // сохранять картинку в jpeg-формате с 85% сжатия. fOut.flush(); fOut.close(); MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); // регистрация в фотоальбоме } catch (Exception e) // здесь необходим блок отслеживания реальных ошибок и исключений, общий Exception приведен в качестве примера { return e.getMessage(); } return ""; } 

The question is actually that this design does not work, what could be the problem? After clicking the Save button, nothing happens.

 public void btnSave_Click(View v){ SavePicture(imageView,folderToSave); } 

Maybe I'm doing something wrong?

    1 answer 1

    You have a crash somewhere, the author of the code carefully wrote about the need for honest catching in the commentary. Try this:

     public void btnSave_Click(View v) { Toast.makeText(SavePicture(imageView, folderToSave)).show(); } 

    And see what the message will show after the click. Most likely, it will be IOEcxeption.

    • /mnt/sdcard/2012821113722.jpg: open failed: EACCES (Permission denied) so that climbs out - Smer4ik
    • and <uses-permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" /> is in the manifest? - Ivan Bartsov
    • M. By the way. If the emulator - check that under the sd-card space is allocated. - Ivan Bartsov
    • added to the manifesto <uses-permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" /> Everything worked, thanks a lot for the hint, it is saved to the sd card root. - Smer4ik