【问题标题】:Gallery Refreshing in androidandroid中的画廊刷新
【发布时间】:2013-12-18 09:22:18
【问题描述】:

我正在从我的应用程序的图库中删除文件。文件已成功删除,但删除后文件仍显示在图库中,直到我重新启动设备。 我尝试过不同的解决方案,例如

       Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
               Uri.fromFile(from.getParentFile()));
          sendBroadcast(intent);

和媒体扫描仪

public class SingalmediaScaner implements MediaScannerConnectionClient {

private MediaScannerConnection mMs;
private File mFile;

public SingalmediaScaner(Context context, File f) {
    mFile = f;
    mMs = new MediaScannerConnection(context, this);
    mMs.connect();
}

@Override
public void onMediaScannerConnected() {
    mMs.scanFile(mFile.toString(), null);
}

@Override
public void onScanCompleted(String path, Uri uri) {
    mMs.disconnect();
}

private static void removeThumbnails(ContentResolver contentResolver,
        long photoId) {
    Cursor thumbnails = contentResolver.query(
            Thumbnails.EXTERNAL_CONTENT_URI, null, Thumbnails.IMAGE_ID
                    + "=?", new String[] { String.valueOf(photoId) }, null);
    for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails
            .moveToNext()) {

        long thumbnailId = thumbnails.getLong(thumbnails
            .getColumnIndex(Thumbnails._ID));
    String path = thumbnails.getString(thumbnails
            .getColumnIndex(Thumbnails.DATA));
    File file = new File(path);
    if (file.delete()) {

        contentResolver.delete(Thumbnails.EXTERNAL_CONTENT_URI,
                Thumbnails._ID + "=?",
                new String[] { String.valueOf(thumbnailId) });

    }

}
}

}

但以上都不适合我 文件已删除,但图库未刷新。 如何在不重新启动设备的情况下刷新图库。 或者我如何在不重新启动我的应用程序的情况下刷新图库

【问题讨论】:

    标签: android gallery


    【解决方案1】:

    这是因为您的图像存储在存储卡中,您需要刷新存储卡才能看到所反映的更改。

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

    【讨论】:

      【解决方案2】:

      我使用了以下方法。

      private void deleteFormGallery(String filepath){
              String[] retCol = { MediaStore.Video.Media._ID };
              Cursor cur = application.getContentResolver().query(
                  MediaStore.Video.Media.EXTERNAL_CONTENT_URI, retCol, 
                  MediaStore.MediaColumns.DATA + "='" + filepath + "'", null, null);
      
              Log.i(TAG, "::deleteFormGallery: count = " + cur.getCount());
              if (cur.getCount() == 0) {
                  return;
              }
              cur.moveToFirst();
              int id = cur.getInt(cur.getColumnIndex(MediaStore.MediaColumns._ID));       
              cur.close();
      
              Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
              int rows = application.getContentResolver().delete(uri, null, null);        
              Log.i(TAG, "::deleteFormGallery: " + id + " deleted rows = "+rows);
          }
      

      【讨论】:

      • 此方法在删除后不刷新图库 ....已删除的项目仍显示在图库中
      • 我遇到了类似的问题。删除文件后,它仍然在图库中显示为损坏的文件。但就我而言,这是一个视频。你可能需要稍微调整一下。日志还说什么?
      • 哦对不起...MediaStore.Images.Media.EXTERNAL_CONTENT_URI....解决我的问题../谢谢Shashika
      【解决方案3】:

      试试这个

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

      【讨论】:

        【解决方案4】:

        我使用以下代码(这也很像我用于创建视频的代码,它还告诉媒体系统有关文件更改并正确更新图库):

        private void deleteVideo(String videoUrl)
        {
            File videoFile = new File(videoUrl);
            if (!videoFile.delete())
            {
                Log.e(TAG, "Failed to delete " + videoUrl);
            }
            else
            {
                MediaScannerConnection.scanFile(mContext,new String[] { videoUrl }, null, new MediaScannerConnection.OnScanCompletedListener() 
                {
                    public void onScanCompleted(String path, Uri uri) 
                    {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-21
          • 1970-01-01
          相关资源
          最近更新 更多