Search code examples
androidkotlinandroid-notifications

How to open a Folder/directory from Notifications in Android


fun showNotification(){

val path = Environment.getExternalStorageDirectory().toString() + "/Android/media/"
val uri = Uri.parse(path)
val intent = Intent(Intent.ACTION_PICK)
intent.setDataAndType(uri, "*/*")

val activityPendingIntent = PendingIntent.getActivity(
    context,
    1,
    intent,
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
)

val notification = NotificationCompat.Builder(context, COUNTER_CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_download_done)
    .setContentTitle("Download Finished")
    .setContentText("Files at Android/media/")
    .setContentIntent(activityPendingIntent)
    .build()

notificationManager.notify(1, notification)

}

here is my code, I only manage to open the folder but I couldn't open the files.

What I wanted to happen is simply redirection to the folder and access the files in there. Is this possible? Am I approaching it wrong?


Solution

  • After a thorough searching, I found an answer that works for me..

    // my notification function
    fun showNotification(){
        
        // an intent to be initialized when:
        lateinit var intent: Intent
    
        // A) Android version is 10 or above
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    
            val sm = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
            intent = sm.primaryStorageVolume.createOpenDocumentTreeIntent()
            var startDir = "Android%2Fmedia" // here is the directory you wanted to open. feel free to test based on your needs.
            var uri: Uri = intent.getParcelableExtra("android.provider.extra.INITIAL_URI")!!
            var scheme = uri.toString()
            scheme = scheme.replace("/root/", "/document/");
            scheme += "%3A$startDir";
            uri = Uri.parse(scheme);
            intent.putExtra("android.provider.extra.INITIAL_URI", uri);
    
         // B) Android Versions 9 and below
        } else {
    
            val path = Environment.getExternalStorageDirectory().toString() + "/Android/media/yourpackagename" // path of the directory you wanted to open
            val uri = Uri.parse(path)
            intent = Intent(Intent.ACTION_PICK)
            intent.putExtra("android.provider.extra.INITIAL_URI", uri)
            intent.setDataAndType(uri, "*/*")
        }
    
    
        val activityPendingIntent = PendingIntent.getActivity(
            context,
            1,
            intent,
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
        )
    
        // shows notification
        val notification = NotificationCompat.Builder(context, COUNTER_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_download_done)
            .setContentTitle("Download Finished")
            .setContentText("Locate at Files:Android/media/net.pregi.netmesh2/NetMesh")
            .setContentIntent(activityPendingIntent)
            .build()
    
        notificationManager.notify(1, notification)
    }
    

    Note that for android 9 and below, I only managed to open the folder, but couldn't manage to open the file inside it. Still looking for an answer to this. For reference, here are the links that I've found related to this matter. Still open for suggestions.

    Android 11 ACTION_OPEN_DOCUMENT_TREE

    How to open specific folder in the storage using intent in android