通过 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); } }); }
相关推荐
异步下载图片保存到sd卡demo. 包括sd卡路径的检查,图片的缓存等
这个功能是实现了网络下载图片,并且保存到本地的SD卡,然后以viewpager的形式显示
绘制手势并保存图片到SD卡,android,使用控件
zynq修改uboot环境变量保存到sd卡。ebaz4205上,启动方式为sd,进入uboot后,修改环境变量可以将环境变量保存到sd卡中。
android保存log到SD卡中
异步将网络图片显示,显示之后,将其复制到sd卡, 复制网络图片的原理是 将drawable 转为byte 数组 再用 FileOutputStream fos fos.write(myByte); 写如sd卡 复制好的图片目录是 sd/tese/a.jpg
Android 从网络上获取图片保存到SD卡中
Android相机、相册获取图片显示并保存到SD卡,并且完美解决拍照裁剪图片完成之后崩溃问题
通过cubemx生成基本配置信息,移植正点原子图片显示实验,将SD卡里面的图片显示到LCD上。用到的硬件资源:单片机:stm32f407vet6,LCD:240*240,flash芯片:w25q128,SD卡:任意TF卡,注意提前将图片放入SD卡中
Gallery从SD卡中获取图片,并显示
NULL 博文链接:https://wang-peng1.iteye.com/blog/669565
Gallery从SD卡中获取图片,并显示完整版
Gallery从SD卡中获取图片,并显示.rar
stm32读取SD卡中图片,并在TFT中显示
AndroidGallery从SD卡中获取图片,并显示