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.