![](/img/trans.png)
[英]android : how to add image from gallery and delete image with a Button
[英]Delete image thumbnail from gallery when it got hidden
之前已經問過這個問題(不是特別喜歡這個)但是還沒有一個All Exclusive答案。 所以我們試圖找到最好的解決方案。 我正在開發一個應用程序,在我的應用程序中,我通過將其文件移動到名為.myPic
的目錄來隱藏名為myPic
的目錄。 當我隱藏我的照片時,它的縮略圖仍然在畫廊中。 我找到了3個解決方案:
使用ACTION_MEDIA_MOUNTED廣泛投射如下:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
這段代碼的問題在於它需要擁抱資源,最重要的是它因為android 4.4而被阻止 。 因此,使用此方法將10張圖片添加到圖庫是不合理的。 所以它不是一個全獨家的方法。 也使用ACTION_MEDIA_SCANNER_SCAN_FILE
也無法在android 4.4上運行
使用MediaScannerConnection
。 所以我創建了一個for
循環並傳遞了我隱藏的每個文件的舊地址。 這是我的MediaScannerConnection
功能:
private void scanFile(File file) {
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
關於MediaScannerConnection
的事情是它只在文件存在時才會生效。 所以我可以說我在myPic
目錄中有一張名為1.jpg
的圖片。 使用這個類我可以立即將1.jpg
添加到我的畫廊但是當我將1.jpg
移動到.myPic
目錄並且我掃描1.jpg
的舊路徑時沒有任何事情發生。 logcat說這個文件不存在。 所以MediaScannerConnection
只將文件添加到圖庫。 如果我將1.jpg
的新路徑傳遞給MediaScannerConnection
怎么MediaScannerConnection
? 以及它增加了1.jpg
從.myPic
目錄庫,並且是完全不是我想要的。 所以再也不是All Exclusive方法
使用getContentResolver()
。 因此,對於刪除縮略圖,此方法可能是最終的解決方案。 所以我寫了吹碼。 在每個循環中,我檢索圖像的路徑並將其傳遞給getContentResolver().delete(Uri.parse(path),null,null)
。 這是代碼:
File myPic = new File(Environment.getExternalStorageDirectory()+"/myPic");
File myPicHide = new File(Environment.getExternalStorageDirectory()+"/.myPic");
if (!(myPicHide.exists()) & !(myPicHide.isDirectory())) {
myPicHide.mkdirs();
};
if (myPic.isDirectory()) {
String[] childeren = myPic.list();
if (childeren.length > 0) {
for (int i = 0; i < childeren.length; i++) {
String fileName = childeren[i];
File from = new File(Environment.getExternalStorageDirectory()+"/myPic"+fileName);
File to = new File(Environment.getExternalStorageDirectory()+"/.myPic"+fileName);
from.renameTo(to);
try {
String path = from.toString();
getContentResolver().delete(Uri.parse(path),null,null);
} catch(Exception e) {
Log.d("Rename", "Error happened");
}
}
}
} else {
Toast.makeText(getApplicationContext(), "myPic directory not found", Toast.LENGTH_LONG).show();
}
但它也不起作用,我的文件的縮略圖仍然顯示在廚房。 所以我用錯誤的方式使用getContentResolver()
? 這可能是所有Exclusive方法,用於刪除文件縮略圖顯示在庫中的情況。 我有我的文件路徑,我只需要從媒體商店內容提供商刪除它。
更新:事實證明在第三個解決方案中使用Uri.parse(path)
是錯誤的。 image Uri以content://
啟動,可由MediaScannerConnection
檢索。 所以我創建了一個名為imageInGalleryUri
的Uri
並為其指定null
值。 使用我的scanFile
函數我scanFile
更改它的值並將其值傳遞給getContentResolver()
。 這是代碼:
boolean whereIsMediaState = true;
Uri imageInGalleryUri = null;
File myPic = new File(Environment.getExternalStorageDirectory()+"/myPic");
File myPicHide = new File(Environment.getExternalStorageDirectory()+"/.myPic");
if (!(myPicHide.exists()) & !(myPicHide.isDirectory())) {
myPicHide.mkdirs();
};
if (myPic.isDirectory()) {
String[] childeren = myPic.list();
if (childeren.length > 0) {
for (int i = 0; i < childeren.length; i++) {
String fileName = childeren[i];
File from = new File(Environment.getExternalStorageDirectory()+"/myPic"+fileName);
scanFile(from);
File to = new File(Environment.getExternalStorageDirectory()+"/.myPic"+fileName);
from.renameTo(to);
if (to.isFile()){
try {
getContentResolver().delete(imageInGalleryUri,null,null);}
catch(Exception e) {
Log.d("Rename", "Error happened");
}
}
}
} else {
Toast.makeText(getApplicationContext(), "myPic directory not found", Toast.LENGTH_LONG).show();
}
private void scanFile(File file) {
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
imageInGalleryUri = uri;
}
});
}
我嘗試了代碼,但它只檢測第一個圖像並從庫中刪除它,但不影響其他圖像。 我無法弄清楚為什么。 任何的想法?
提前謝謝你的幫助
。 文件夾之前只是讓它不可見。 但是有辦法說不要使用這個文件夾來畫廊。 請嘗試將名為“.nomedia”文件的空文件放入您的文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.