I am trying to delete image from device by using file.delete()
. Delete method is being called and even it is returning true
but when I open my gallery image is still there.
My image path is :
/data/user/0/com.myappname/app_myappname/20180413__164222_0.7680390806195996.png
Please have a look what i have tried.
1)
File file = new File(path);
if (file.exists()) {
file.delete();
}
2)
try {
File file = new File("file://" + path);
String getDirectoryPath = file.getParent(); // Only return path if physical file exist else return null
File fileNew = new File(getDirectoryPath);
fileNew.delete();
} catch (Exception e) {
}
3)
File file = mContext.getFilesDir(); // this will get you internal directory path
Log.d("BLA BLA", file.getAbsolutePath());
File newfile = new File(file.getAbsolutePath() + imagePath); // foo is the directory 2 create
newfile.delete();
4)
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
File fileName = new File(data, imagePath);
if (fileName.exists())
{
fileName.delete();
}
}
} catch (Exception e) {
}
I have applied almost every solution from stack overflow related to file.delete()
. But nothing is working ? Any help or clue will be appreciated.
Edit:
I have refreshed the gallery when delete()
returns true
using this method but still image is present in app gallery.
Look at my code:
public void callBroadCast() {
if (Build.VERSION.SDK_INT >= 14) {
Log.e("-->", " >= 14");
MediaScannerConnection.scanFile(mContext, new String[]{Environment.getExternalStorageDirectory().toString()}, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
} else {
Log.e("-->", " < 14");
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}