Skip to content Skip to sidebar Skip to footer

Save Image On External Storage

Hey I want to save an image in the external storage. I got two versions out of here but both aren't working. Goal is that the user clicks a button and the image is saved and then t

Solution 1:

Try this

 AsyncTask fileTask = new AsyncTask() {
        @Override
        protectedObject doInBackground(Object[] objects) {
File directory = new File(Environment.getExternalStorageDirectory() + File.separator + "MyApplication");
if (!directory.exists()) {
                    directory.mkdirs();
                }
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String name = " "+n+".jpg";
File pictureFile = new File(directory, name);
pictureFile.createNewFile();
try {
FileOutputStream out = new FileOutputStream(pictureFile);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.close();
} catch (Exception e) {
       e.printStackTrace();
}
returnnull;
        }
    };
    fileTask.execute();

This should work fine provided all the file handling permissions are available for your app.

Also please note that you should never do file operations in the main thread thus always use AsyncTask if you haven't already done so.

Solution 2:

Late but might be helpful

privatevoidsaveimage(Bitmap img) {


                try {
                    StringimageName=newSimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(newDate());
                    booleanimageSaved=false;
                    if (!(img == null || img.isRecycled())) {
                        ContentResolvercontentResolver= getContentResolver();
                        Filefile=newFile(getStoragePath() + "img path/");
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        FileimageFile=newFile(file, String.format("%s.png", newObject[]{imageName}));
                        try {
                            FileOutputStreamout=newFileOutputStream(imageFile);
                            imageSaved = img.compress(CompressFormat.PNG, 100, out);
                            if (out != null) {
                                out.flush();
                                out.close();
                            }
                        } catch (Exception e) {
                            Log.e(TAG, "Unable to write the image to gallery", e);
                        }
                        ContentValuesvalues=newContentValues(8);
                        values.put(SettingsJsonConstants.PROMPT_TITLE_KEY, imageName);
                        values.put("_display_name", camera.name);
                        Stringlocation="";
                        if (camera.country != null && camera.country.length() > 0) {
                            location = camera.country;
                        }
                        if (camera.city != null && camera.city.length() > 0) {
                            location = location + ", " + camera.city;
                        }
                        values.put("description", location);
                        values.put("mime_type", "image/png");
                        values.put("description", "");
                        longmillis= System.currentTimeMillis();
                        values.put("date_added", Long.valueOf(millis / 1000));
                        values.put("datetaken", Long.valueOf(millis));
                        values.put("_data", imageFile.getAbsolutePath());
                        contentResolver.insert(Media.EXTERNAL_CONTENT_URI, values);
                        MediaScannerConnection.scanFile(getApplicationContext(), newString[]{imageFile.getPath()}, newString[]{"image/png"}, newOnScanCompletedListener() {
                            publicvoidonScanCompleted(String path, Uri uri) {
                            }
                        });
                    }
                    if (imageSaved) {

                    }
                } catch (Exception e2) {
                    Log.e(TAG, "saveSnapshot", e2);
                }




    }

Post a Comment for "Save Image On External Storage"