下面列出了android.content.Intent#ACTION_MEDIA_SCANNER_SCAN_FILE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onScanCompleted(String path, Uri uri) {
Log.i("SingleMediaScanner", path);
Log.i("SingleMediaScanner", uri.getPath());
mMs.disconnect();
MediaScannerConnection.scanFile(mContext, new String[] { mFile.getAbsolutePath() }, new String[] { "video/*" }, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(mFile));
mContext.sendBroadcast(mediaScanIntent);
}
else
{
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(mFile.getAbsolutePath())));
}
}
public void importWallets(Context c, ArrayList<File> toImport) throws Exception {
for (int i = 0; i < toImport.size(); i++) {
String address = stripWalletName(toImport.get(i).getName());
if (address.length() == 40) {
copyFile(toImport.get(i), new File(c.getFilesDir(), address));
if (!BuildConfig.DEBUG)
toImport.get(i).delete();
WalletStorage.getInstance(c).add(new FullWallet("0x" + address, address), c);
AddressNameConverter.getInstance(c).put("0x" + address, "Wallet " + ("0x" + address).substring(0, 6), c);
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(toImport.get(i)); // With 'permFile' being the File object
mediaScannerIntent.setData(fileContentUri);
c.sendBroadcast(mediaScannerIntent); // With 'this' being the context, e.g. the activity
}
}
}
public static void updateMediaStore(Context context, ArrayList<DocumentInfo> docs, String parentPath) {
try {
if(Utils.hasKitKat()){
ArrayList<String> paths = new ArrayList<>();
for(DocumentInfo doc : docs){
paths.add(parentPath + File.separator + doc.displayName);
}
String[] pathsArray = paths.toArray(new String[paths.size()]);
FileUtils.updateMediaStore(context, pathsArray);
}
else{
Uri contentUri = Uri.fromFile(new File(parentPath).getParentFile());
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri);
context.sendBroadcast(mediaScanIntent);
}
}
catch (Exception e){
e.printStackTrace();
}
}
public static void addMediaToGallery(Uri uri) {
if (uri == null) {
return;
}
try {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
ApplicationLoader.applicationContext.sendBroadcast(mediaScanIntent);
} catch (Exception e) {
FileLog.e(e);
}
}
public static void addMediaToGallery(Uri uri) {
if (uri == null) {
return;
}
try {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
ApplicationLoader.applicationContext.sendBroadcast(mediaScanIntent);
} catch (Exception e) {
FileLog.e(e);
}
}
public static void addMediaToGallery(Uri uri) {
if (uri == null) {
return;
}
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
ApplicationLoader.applicationContext.sendBroadcast(mediaScanIntent);
}
public static void addFileToMediaStore(final String path, Context context) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file = new File(path);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
private void addPictureToGallery(@NonNull Context context, @NonNull String photoPath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
try {
File f = new File(photoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
} catch (NullPointerException e) {
Log.e(TAG, "Error opening file: " + photoPath);
}
}
private void galleryAddPic(String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
image_path = Uri.fromFile(f);
mediaScanIntent.setData(image_path);
mContext.sendBroadcast(mediaScanIntent);
}
public boolean addSignatureToGallery(Bitmap signature) {
boolean result = false;
try {
File photo = new File(getAlbumStorageDir("SignaturePad"), String.format("Signature_%d.jpg", System.currentTimeMillis()));
saveBitmapToJPG(signature, photo);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(photo);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
result = true;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private void setImageToView(){
//Save the file in gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
// Get the dimensions of the view
int targetH = imageView.getMaxHeight();
int targetW = imageView.getMaxWidth();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoH = bmOptions.outHeight;
int photoW = bmOptions.outWidth;
// Determine how much to scale down image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
imageView.setImageBitmap(imageBitmap);
// enable lock task button
lockTaskButton.setEnabled(true);
}
/**
* Add a picture to the media store (via scanning).
*
* @param path the path of the image.
*/
public static void addPictureToMediaStore(@NonNull final String path) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file = new File(path);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
Application.getAppContext().sendBroadcast(mediaScanIntent);
}
/**
* 通过广播的方式通知系统扫描某个文件
* @param appContext appContext
* @param filePath 要被扫描的文件路径
*/
public static void notifySysScanFile(Context appContext, String filePath) {
Uri data = Uri.parse("file://" + filePath);
Intent toScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data);
appContext.sendBroadcast(toScanIntent);
}
private static void sendPictureStoredBroadcast(Context context, String qrSavePath) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(new File(qrSavePath));
intent.setData(uri);
context.sendBroadcast(intent);
}
/**
* Send broadcast of new file so files appear over MTP
*/
private void broadcastNewFile(Uri nativeUri) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, nativeUri);
context.sendBroadcast(intent);
}
private static void scanFile(Context context, String filePath) {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(new File(filePath)));
context.sendBroadcast(scanIntent);
}
public static void updateMediaScanner(Context context, Uri uri) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
context.sendBroadcast(mediaScanIntent);
}
/**
* Notifies system there is a new media file, so that it appears in photo galleries immediately.
*/
private static void updateMediaStore(Context context, File file) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
context.sendBroadcast(intent);
}
public static void updateMedia(Context ctx, File file) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file));
ctx.sendBroadcast(intent);
}
/**
* 通知媒体库更新文件
* @param context
* @param filePath 文件全路径
*
* */
public static void scanFile(Context context, String filePath) {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(new File(filePath)));
context.sendBroadcast(scanIntent);
}