I am using HTC M9 with Android 5.0.2 and I have an image that I display on the screen in an ImageView.
On a button click, I am save the image locally on the device. And on another button click, I am trying to delete the image from my device and from my container.
I am using the accepted and in Saving and Reading Bitmaps/Images from Internal memory in Android to save and retrieve the image:
On Save_ButtonClick:
private void saveToInternalStorage(Bitmap bitmapImage, int ImageNumber){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File MyPath = new File(MyDirectory,"Image" + ImageNumber + ".jpg");
// Add the path to the container
ImagePathes.add("Image" + ImageNumber + ".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(MyPath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//return MyDirectory.getAbsolutePath();
}
I tried the answers in Delete file from internal storage and ImagesPathes
still does not change, and I still see the image on the screen. I am using this code to delete my file:
public void DeleteImage(View view)
{
try
{
// remove the file from internal storage
ContextWrapper cw = new ContextWrapper(this);
File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
String path = MyDirectory.getAbsolutePath();
File fileToBeDeleted = new File(getFilesDir(), ImagesNames.get(SelectedIndex)); // current image from my ArrayList - Bitmap
boolean WasDeleted = fileToBeDeleted.delete();
//Or
//File dir = getFilesDir();
//File file = new File(dir, ImagesNames.get(SelectedIndex));
//boolean deleted = file.delete();
// remove file name from my array
ImagesNames.remove(SelectedIndex);
SelectedIndex++;
} catch (Exception e)
{
System.err.println(e.toString());
}
}
There is no error, it is just that WasDeleted
is always false and ImagesPathes
does not change. What is the problem?
Edit:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kkhalaf.mpconbot.test">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.kkhalaf.mpconbot"
android:handleProfiling="false"
android:functionalTest="false"
android:label="Tests for com.example.kkhalaf.mpconbot"/>
</manifest>
Path when Saving: /data/data/com.example.kkhalaf.mpconbot/files/Image0.jpg
Path when Deleting: /data/data/com.example.kkhalaf.mpconbot/files/Image0.jpg