2

My app takes photo from camera and saves it in a file whose Uri is stored in SQL database. Initializing bitmap using Uri from database works flawlessly. However, when i try to initialize a file using Uri from database and then delete using imagefile.delete()it does not work. I have tried few methods given in other posts but none worked.

This i how i save file.

Intent for Camera:

Intent startCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (startCamera.resolveActivity(getPackageManager())!=null)
                    {
                        photoFile= null;
                        try{
                            photoFile = createImageFile();
                        }
                        catch (IOException ex)
                        {
                            Log.e("File Creation","error");
                        }
                        if (photoFile!=null)
                        {
                            photoURI = FileProvider.getUriForFile(context,"lcukerd.com.android.fileprovider",photoFile);
                            startCamera.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                            camerastarted=true;
                            startActivityForResult(startCamera,CAMERA_REQUEST);
                        }
                    }

Declared method:

private File createImageFile() throws IOException
{
    String EName = "Stuff";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(EName,".jpg",storageDir);
    return image;
}

Compressing image:

photoFile.delete();
FileOutputStream out = null;
                    try {
                        File image = createImageFile();
                        photoURI = FileProvider.getUriForFile(context, "lcukerd.com.android.fileprovider", image);
                        out = new FileOutputStream(image);
                        photo.compress(Bitmap.CompressFormat.PNG, 100, out);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (out != null) {
                                out.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

saving Uri to database is simple conversion of Uri to string and then saving in TEXT column of table.

Help me delete image file .

Edit:

I tried following.

private void deleteimage(String imageloc)
    {
        File imagefile = new File(getRealPathFromURI(Uri.parse(imageloc)));
        Log.d("file deletion",String.valueOf(imagefile.delete())+" "+imageloc);
    }
    public String getRealPathFromURI( Uri contentUri) {
        String result;
        Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentUri.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }

and..

private void deleteimage(String imageloc)
    {
        File imagefile = new File(Uri.parse(imageloc).getpath());
        Log.d("file deletion",String.valueOf(imagefile.delete())+" "+imageloc);
    }

None worked.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
lcukerd
  • 83
  • 1
  • 10

1 Answers1

9

Since this is a Uri that you are getting from FileProvider, call delete() on a ContentResolver, passing in the Uri (plus null for the remaining parameters). That will delete the underlying file, at least according to the FileProvider documentation.

Or, store the file path, rather than the Uri, in your database, since this is your file. You can recreate the Uri later as needed using FileProvider.getUriForFile(), and you can use delete() on File to delete the file, given its path.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I don't understand . What should i initialize FileProvider variable with? This doesn't work. FileProvider imagefile = new FileProvider(); imagefile.delete(Uri.parse(imageloc),null,null); – lcukerd Mar 18 '17 at 18:18
  • @lcukerd: The first paragraph of my answer says to call `delete()` **on a `ContentResolver`**. You get a `ContentResolver` by calling `getContentResolver()` on some `Context` (e.g., an `Activity`). – CommonsWare Mar 18 '17 at 18:24
  • Thanks again, it worked . I almost forgot about ContentResolver even though I used it in another activity :( – lcukerd Mar 18 '17 at 18:31