1

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

anatol
  • 791
  • 9
  • 16
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • http://stackoverflow.com/questions/3554722/how-to-delete-internal-storage-file-in-android – Jayakrishnan Dec 03 '16 at 02:00
  • Sorry forgot to mention that this also does not work. I still see the image on the screen and `ImagePathes` is not changing. I will edit, thanks – Khalil Khalaf Dec 03 '16 at 02:06
  • are you sure image is saving correctly (make sure permissions are given in manifest) in save button click. If so please log all values with index of ImagesNames array. After that log selected index in delete method. please make sure the path you are adding to array is absolute path. – Jayakrishnan Dec 03 '16 at 02:14
  • Yes I gave permission in the Manifest file and I see the image on my screen see edited please. what do you mean by log? I am keeping `ArrayList ImagesNames` as the filenames and index of the selected Image in the `Gallery` view of the screen (I retrieve it based on its name and display it on the screen) – Khalil Khalaf Dec 03 '16 at 02:29
  • i mean , File fileToBeDeleted = new File(getFilesDir(), ImagesNames.get(SelectedIndex)); path = fileToBeDeleted.getAbsolutePath(); Log.d("file path to delete", path ); make sure the path is correct. – Jayakrishnan Dec 03 '16 at 02:36
  • @JayakrishnanPm The issue was fixed because of your help. Thank you – Khalil Khalaf Dec 09 '16 at 02:27

1 Answers1

0

It turned out that my problem is on this line:

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)); // here
boolean WasDeleted = fileToBeDeleted.delete();

And I should have done this instead:

ContextWrapper cw = new ContextWrapper(this);
File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
String path = MyDirectory.getAbsolutePath();
File fileToBeDeleted = new File(path + "//Image" + SelectedIndex + ".jpg"); // current image
boolean WasDeleted = fileToBeDeleted.delete();
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104