保存图片到SD卡,但是Gallery中不能及时显示的问题

通过 Intent.ACTION_MEDIA_MOUNTED 进行整个SD卡扫描:

public void sdScan(){     
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" 
                    + Environment.getExternalStorageDirectory())));     
    }

 The method is restricted for only system apps from 4.4.

here is the another solution..pass the path of the image you have deleted or added and if the image is deleted pass true or if added then pass false.

   /**
 * Scanning the file in the Gallery database
 * 
 * @param path
 * @param isDelete
 */
private void scanFile(String path, final boolean isDelete) {
    try {
        MediaScannerConnection.scanFile(context, new String[] { path },
                null, new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        if (isDelete) {
                            if (uri != null) {
                                context.getContentResolver().delete(uri,
                                        null, null);
                            }
                        }
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

}

 

 

 通过 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 扫描某个文件:

 public void fileScan(String filePath){     
        Uri data = Uri.parse("file://"+filePath);     
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));     
    }

 

从mediaStore中删除。

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});
        }
    }
}

 

You can use following technique to update all files present in a single folder:

for (File child : fileFolder.listFiles()) {
    if (child.isFile()) {
        fName = child.getName();
        Log.d("MyTag", "Scanning >> " + child.getName());

    MediaScannerConnection
        .scanFile( MyActivity.this,
        new String[] { "path/of/our/folder" + fName },
        null,  new MediaScannerConnection.OnScanCompletedListener() {
              public void onScanCompleted(
              String path, Uri uri) {
                 Log.i("ExternalStorage",  "Scanned " + path + ":");
                 Log.i("ExternalStorage",  "-> uri=" + uri);
              }
        });
    }
}

  

删除image并且更新Media

// Set up the projection (we only need the ID)
String[] projection = { MediaStore.Images.Media._ID };

// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { file.getAbsolutePath() };

// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
    // We found the ID. Deleting the item via the content provider will also remove the file
    long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
    Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
    contentResolver.delete(deleteUri, null, null);
} else {
    // File not found in media store DB
}
c.close();

 

手动刷新:

public void getAllUri() {
		Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Images.Media.DEFAULT_SORT_ORDER);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			String data = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
			File file = new File(data);
			scanFile(file);
			cursor.moveToNext();
		}
		
		Cursor cursor2 = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
		cursor2.moveToFirst();
		while(!cursor2.isAfterLast()){
			String path = cursor2.getString(cursor2.getColumnIndex(MediaStore.MediaColumns.DATA));
			File file = new File(path);
			scanFile(file);
			cursor2.moveToNext();
		}
	}

	private void scanFile(File file) {
		MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
			@Override
			public void onScanCompleted(String path, Uri uri) {
				Log.i("ExternalStorage", "Scanned " + path + ":");
				Log.i("ExternalStorage", "-> uri=" + uri);
			}
		});
	}

 

 

 

转载:http://hxsdit.com/1647

 http://blog.csdn.net/happy08god/article/details/9303715

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值