我正在开发一个Android应用程序,它在一个活动中从库中选择一个图像并将其显示在另一个活动中。但是当我尝试删除所选图像时它不会删除。我在两个活动之间传递它的uri。非常感谢提前!!!!
这是我的代码:
ACTIVITY HOMESCREEN
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
Intent i = new Intent(this, Imageviewer.class);
i.putExtra("imgpath", uri.toString());
startActivity(i);
}
}
IMAGEVIEWER ACTIVITY:
Uri imageUri;
imageUri = Uri.parse(intent.getStringExtra("imgpath"));
File fdelete = new File(imageUri.toString());
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" );
} else {
System.out.println("file not Deleted :");
}
}
答案 0 :(得分:2)
首先,您必须采用真实的图像路径:
//getting real path from uri
private String getFilePath(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String picturePath = cursor.getString(columnIndex); // returns null
cursor.close();
return picturePath;
}
return null;
}
然后您可以删除此文件,如下所示:
Uri imageUri;
imageUri = Uri.parse(intent.getStringExtra("imgpath"));
File fdelete = new File(getFilePath(imageUri));
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" );
} else {
System.out.println("file not Deleted :");
}
}
答案 1 :(得分:0)
在您的情况下尝试:(在第二个FinalRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(FinalRow, LastColumn)), , xlYes).Name = "Data"
)Activity
。
ImageViewerActivity
您也可以逐行调试代码,查看并检查Intent intent = getIntent();
String receivedPath = intent.getExtras().getString("imgpath");
File fdelete = new File(receivedPath);
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" );
} else {
System.out.println("file not Deleted :");
}
}
是否正确无误!