How to delete Media File Folder in Anadroid?
Try to this method..
public void DeleteMediaFile(Uri fileUri){ File file = new File(fileUri.getPath()); file.delete(); if(file.exists()){ try { file.getCanonicalFile().delete(); if(file.exists()){ getApplicationContext().deleteFile(file.getName()); } } catch (IOException e) { e.printStackTrace(); } } }
}
In this class camera capture image and then image is change in bitmap and delete this file.
..........................................................................................
public class MainActivity extends AppCompatActivity {
private Uri fileUri;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String IMAGE_DIRECTORY_NAME = "Camera";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button takepic=(Button)findViewById(R.id.button); takepic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
} }); gridView=(GridView)findViewById(R.id.gridView); }
public Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); }
private static File getOutputMediaFile(int type) { // External sdcard location File mediaStorageDir = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME); // Create a media file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",File mediaFile; if (type == MEDIA_TYPE_IMAGE) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".bmp"); } else { return null; } return mediaFile;// returen media file }
public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { if (resultCode == RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);
// whene delete fileUri folder from android device…
DeleteMediaFile(fileUri);
}
}
}
}
}
public void DeleteMediaFile(Uri fileUri){ File file = new File(fileUri.getPath()); file.delete(); if(file.exists()){ try { file.getCanonicalFile().delete(); if(file.exists()){ getApplicationContext().deleteFile(file.getName()); } } catch (IOException e) { e.printStackTrace(); } } }
}
}
0 comments