【问题标题】:how update Android MediaStore after i move a JPG file移动 JPG 文件后如何更新 Android MediaStore
【发布时间】:2012-08-08 00:58:27
【问题描述】:

我的应用程序将 .jpg 文件移动到其他文件夹并在图库中查看我有 sendBroadcast ACTION_MEDIA_MOUNTED

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + 
            Environment.getExternalStorageDirectory() ))); 

但这需要很多时间.. 我明白(也许)我必须直接在 mediaStore 中使用 cursor/contentResolver 手动更新才能更快地获得这个。谁可以帮我这个事?谢谢.. 我的代码实际上是:

 Uri uri = (Uri) list.get(cont); 
 Cursor cursor = managedQuery(uri, proj, null, null, null);
 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
 cursor.moveToFirst();
 String app = cursor.getString(column_index);
 File orig =  new File( app.toString());
 File dest = new File( destination_path +"/"+ orig.getName().toString());
 orig.renameTo(dest);

有了这个,我将一个文件从一个路径移动到另一个路径。

在此之后,在图库中获取图像 我必须发送广播 ACTION_MEDIA_MOUNTED

【问题讨论】:

    标签: android image move mediastore


    【解决方案1】:

    使用 MediaScannerConnection 更新操作系统:

    MediaScannerConnection.scanFile(this,
              new String[] { file.toString() }, null,
              new MediaScannerConnection.OnScanCompletedListener() {
          public void onScanCompleted(String path, Uri uri) {
              // code to execute when scanning is complete
          }
     });
    

    【讨论】:

    • file 是新建的文件吗?我已经尝试过这段代码......但是在画廊中我有新照片(正确)和灰色图像,旧路径文件夹作为文件夹名称......就像旧的 jpg 文件不会从媒体存储中自动删除......
    • 是的,在您的情况下,“文件”将是“目标”。 AFIAK,更新已删除文件的唯一可靠方法是通过较慢的媒体装载广播。
    【解决方案2】:

    我只需要这样做,使用以下更新MediaStore

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, newPath);
    boolean successMediaStore = context.getContentResolver().update(
        MediaStore.<TYPE>.Media.EXTERNAL_CONTENT_URI, values,
        MediaStore.MediaColumns.DATA + "='" + oldPath + "'", null) == 1;
    

    &lt;TYPE&gt; 替换为正确的媒体存储...(ImagesVideoAudio...)

    【讨论】:

    • 如果您需要即时结果,请使用此方法,而不是 MediaScannerConnection。
    猜你喜欢
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2020-01-07
    • 2011-03-19
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多