skip to Main Content

I have saved a video in getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
path in the android private directory. I want to show the video file in the gallery. I have tried MediascannerConnection.scanFile but it did not work.
Is there any altermative?

3

Answers


  1. getExternalFilesDir() 
    

    is a private location for your app.

    The MediaStore will not scan files from that location.

    And hence no Gallery app will know about them as Gallery apps query the MediaStore.

    Login or Signup to reply.
  2. you have to save your file in shared storage, for example, if u want to save pics, save them in /pictures folder or videos save them in videos folder

    //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, this issue rises in scoped storage only, i.e sdk > 28 so start using scoped storage

    Give this a read (Storage changes in android 11)

    Login or Signup to reply.
  3. You can use the MediaStore api to load a video from the gallery.

            // 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
            }
    

    This code is for loading all videos from the downloads collection, if you want a specific one you can change the value of the selection variable to match only the specific video.

    After retrieving the desired content uri, you can load the video thumbnail into an ImageView using Glide:

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

    Or just use a VideoView and set the content uri as the video uri to play the video.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search