【问题标题】:How to add video to Media Store database?如何将视频添加到媒体商店数据库?
【发布时间】:2021-08-12 06:12:32
【问题描述】:

我在 getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) 中保存了一个视频 android私有目录中的路径。我想在图库中显示视频文件。我试过 MediascannerConnection.scanFile 但没有用。 有没有替代方案?

【问题讨论】:

标签: java android android-studio mediastore scoped-storage


【解决方案1】:

您必须将文件保存在共享存储中,例如,如果您要保存图片,请将它们保存在 /pictures 文件夹中,或者将视频保存在视频文件夹中

//for pictures
 contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,Environment.DIRECTORY_PICTURES + "/your app folder");
//for videos
 contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,Environment.DIRECTORY_MOVIES + "/your app folder");

P.s,这个问题只出现在范围存储中,即 sdk > 28 所以开始使用范围存储

Give this a read (Storage changes in android 11)

【讨论】:

    【解决方案2】:
    getExternalFilesDir() 
    

    是您的应用的私人位置。

    MediaStore 不会从该位置扫描文件。

    因此,当 Gallery 应用程序查询 MediaStore 时,没有 Gallery 应用程序会知道它们。

    【讨论】:

      【解决方案3】:

      您可以使用 MediaStore api 从图库中加载视频。

              // Represent the downloads collection
              Uri downloadsCollection;
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // if sdk is 29 or higher
                  downloadsCollection = MediaStore.Downloads.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
              } else { // if sdk is lower than 29
                  downloadsCollection = MediaStore.Downloads.Media.EXTERNAL_CONTENT_URI;
              }
      
              /** Represents the list of which columns to return from query */
              String[] projection = {
                  MediaStore.DownloadColumns._ID,
                  MediaStore.DownloadColumns.DATA,
                  MediaStore.DownloadColumns.WIDTH,
                  MediaStore.DownloadColumns.HEIGHT,
                  MediaStore.DownloadColumns.DISPLAY_NAME,
                  MediaStore.DownloadColumns.DATE_MODIFIED,
                  MediaStore.DownloadColumns.SIZE
                 };
      
              /** Initializing the selection file types to select from the gallery */
              String selection = MediaStore.DownloadColumns.MEDIA_TYPE + "=" + MediaStore.DownloadColumns.MEDIA_TYPE_VIDEO;
      
              /** Initializing the order to sort the gallery by - date added descending */
              val orderBy = MediaStore.DownloadColumns.DATE_ADDED + " DESC";
      
              // Querying the gallery files collection, with the projection columns, the selection, and the order to sort by
              Cursor cursor = getContentResolver().query(collection, projection, selection, null, orderBy);
                 
              /** Represents the id column */
              int idColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns._ID)
      
              /** Represents the width column */
              int widthColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.WIDTH)
      
              /** Represents the height column */
              int heightColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.HEIGHT)
      
              /** Represents the display name column */
              int displayNameColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.DISPLAY_NAME)
      
              /** Represents the date added column */
              int dateAddedColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.DATE_MODIFIED)
      
              /** Represents the size column */
              int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.SIZE)
      
              /** Represents the path to the file on disk column */
              int filePathColumnIndex = cursor.getColumnIndex(MediaStore.DownloadColumns.DATA)
      
              while (cursor.moveToNext()) { // While there are files in the gallery
                  /** Represents the id of the current file */
                  val id = cursor.getLong(idColumn)
      
                  /** Represents the value which is the uri related file local path */
                  val filePath = it.getString(filePathColumnIndex)
      
                  /** Represents the display name of the current file */
                  val displayName = it.getString(displayNameColumn)
      
                  /** Represents the width of the current file */
                  val width = it.getInt(widthColumn)
      
                  /** Represents the height of the current file */
                  val height = it.getInt(heightColumn)
      
                  /** Represent the date added of the current file in milliseconds */
                  val dateAdded = it.getLong(dateAddedColumn)
      
                  /** Represents the size of the current file */
                  var size = it.getLong(sizeColumn)
      
                  /** Represents the uri of the current file */
                  val contentUri = ContentUris.withAppendedId(downloadsCollection, id)
      
                  // todo: you can create a model object that will save all these values and then add it to a list
              }
      

      此代码用于加载下载集合中的所有视频,如果您想要特定的视频,您可以更改 selection 变量的值以仅匹配特定视频。

      检索到所需的内容 uri 后,您可以使用 Glide 将视频缩略图加载到 ImageView

      Glide.with(context).load(contentUri).into(imageView)
      

      或者只是使用 VideoView 并将内容 uri 设置为视频 uri 以播放视频。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-24
        • 2019-01-19
        • 2015-03-30
        • 2012-12-17
        相关资源
        最近更新 更多