Reputation: 188
I have a piece of code which basically crops an image selected by the user and is supposed to overwrite the image "CurrentProfilePic.jpg" with the new image.It does so by first deleting the image if it exists and then creating it again.but the image doesnt get deleted.So the no. of times the code is run, that many no. of images are created WITH THE SAME NAME. I used logs to see if the file.delete(); returns true, and it does return true.
public void cropImage(View v) {
bitmap=cropImageView.getCroppedImage();
boolean imageSaved = false;
String imageName="CurrentProfilePic";
//to save current image to directory
if (bitmap != null && !bitmap.isRecycled()) {
File storagePath = new File(
Environment.getExternalStorageDirectory() + "/SimpleMessaging/");
if (!storagePath.exists()) {
storagePath.mkdirs();
}
File temp= new File(Environment.getExternalStorageDirectory() + "/SimpleMessaging/CurrentProfilePic.jpg");
if(temp.exists()){
boolean x= temp.delete();
Log.d("PICS", "Inside if exist of pic");
if(x)
Log.d("bool", "x true");
else
Log.d("bool", "x false");
}
FileOutputStream out = null;
File imageFile = new File(storagePath, String.format("%s.jpg",
imageName));
try {
out = new FileOutputStream(imageFile);
imageSaved = bitmap.compress(Bitmap.CompressFormat.JPEG,
100, out);
out.flush();
out.close();
} catch (Exception e) {
Log.e("SaveToSD ", "Unable to write the image to gallery" + e);
}
ContentValues values = new ContentValues(3);
values.put(Images.Media.TITLE, imageName);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put("_data", imageFile.getAbsolutePath());
getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
}
startActivity(new Intent(getBaseContext(), EditProfilePic.class));
finish();
}
The thing to stress upon is that the latest image is overwritten on each of those files, but their sizes are unchanged, their original sizes.
Upvotes: 0
Views: 243
Reputation: 1006724
Delete getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
and its supporting code. Use MediaScannerConnection
or ACTION_MEDIA_SCANNER_SCAN_FILE
to index your file.
Upvotes: 1