Problem in deleting image from its path. really confused in deleting it from the application as well from the gallery I'm facing problem in deleting image from image View hot to delete it from the activity as well as the external media of the file location.
Trying it since 3 days and found no solution yet. I need to apply the delete button code in this java file
public class FullScreenViewActivity extends Activity {
private Utils utils;
private FullScreenImageAdapter adapter, image;
private ViewPager viewPager;
Button btnClose, btnShare, btnDelete;
private static Context mContext;
ContentResolver contentResolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
viewPager = (ViewPager) findViewById(R.id.pager);
utils = new Utils(getApplicationContext());
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
utils.getFilePaths());
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
btnClose = (Button) findViewById(R.id.btnClose);
btnShare = (Button) findViewById(R.id.btnshare0);
btnDelete = (Button) findViewById(R.id.btndelete);
btnClose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
btnShare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File file = new File(adapter._imagePaths.get(viewPager
.getCurrentItem()));
Intent mShareIntent = new Intent(Intent.ACTION_SEND);
mShareIntent.setType("image/*");
mShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(mShareIntent, "Shareith:"));
}
});
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
deleteTmpFile(viewPager.getCurrentItem());
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("Delete this Photo?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
});
}
public void deleteTmpFile(int pos) {
//String Foldername = mContext.getResources()
// .getString(R.string.app_name);
/*
* String filepath = Environment.getExternalStorageDirectory().getPath()
* + "/" + Foldername + "/" +data.get(pos) ;
*/
File f = new File(adapter._imagePaths.get(viewPager
.getCurrentItem()));
//File f = new File(filepath);
if (f.exists()) {
f.delete();
deleteFileFromMediaStore(mContext.getContentResolver(), f);
// final Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
// mContext.getContentResolver().delete(uri,
// MediaStore.MediaColumns.DATA + " =?",
// new String[] { filepath });
notifyAll();
}
Toast.makeText(mContext, "Delete Successfully..", Toast.LENGTH_SHORT)
.show();
}
public static void deleteFileFromMediaStore(
final ContentResolver contentResolver, final File file) {
String canonicalPath;
try {
canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
canonicalPath = file.getAbsolutePath();
}
final Uri uri = MediaStore.Files.getContentUri("external");
final int result = contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?",
new String[] { canonicalPath });
if (result == 0) {
final String absolutePath = file.getAbsolutePath();
if (!absolutePath.equals(canonicalPath)) {
contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA
+ "=?", new String[] { absolutePath });
}
}
}
}