Android Nerd
Android Nerd

Reputation: 635

Open a Directory of Downloads by Clicking the Notification

Hello there I am newbie in Android Development. What I am trying to do is:

  1. Downloading a file using a URL
  2. Placed it in my downloads
  3. Used a Download Manager within IntentService for downloading
  4. Upon completion of the downloading, I am showing a Notification
  5. The notification notify's the user for the download completion

The problem is that I want the user to tap on the generated notification, and to open the Downloads Folder!

What I have been trying is:

public void sendNotification() {

        // Use NotificationCompat.Builder to set up our notification.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        //icon appears in device notification bar and right hand corner of notification
        builder.setSmallIcon(R.drawable.downloaded);

        // This intent is fired when notification is clicked
        *****Here I want the user if clicks this notification the Download Folder should Open up*****
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

        Uri imgUri = Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));

        intent.setDataAndType(imgUri, "file/*");
        startActivity(intent);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingIntent);

        // Large icon appears on the left of the notification
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.downloaded));

        // Content title, which appears in large type at the top of the notification
        builder.setContentTitle("Complete");

        // Content text, which appears in smaller text below the title
        builder.setContentText("Your Download has been completed Successfully!");

        //set the subtext
        builder.setSubText("Click to open the Downloads Folder!");
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

Whenever I clicked on it nothing happend!

I am new to Android, kindly review my mistake so that I can learn more. This is not an assignment, I am learning it to myself! Thanks in advance.

Upvotes: 1

Views: 4448

Answers (3)

Javier Diaz
Javier Diaz

Reputation: 119

I did it, this way:

// Create an explicit intent for an Activity in your app
    val intent = Intent(DownloadManager.ACTION_VIEW_DOWNLOADS).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
    builder = NotificationCompat.Builder(context!!, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_file_word)
        .setContentTitle("Descarga completa")
        .setContentText("Descarga de archivo completa")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent)
        .setAutoCancel(true)

Upvotes: 1

mdDroid
mdDroid

Reputation: 3195

check this

public void openFolder()
{
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/myFolder/");
  intent.setDataAndType(uri, "text/csv");
  startActivity(Intent.createChooser(intent, "Open folder"));
}

Upvotes: 0

Sohail Zahid
Sohail Zahid

Reputation: 8149

Try like this

    Uri selectedUri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(selectedUri, "resource/folder");

    if (intent.resolveActivityInfo(getPackageManager(), 0) != null){
            startActivity(intent);
        }
        else 
        {
          // if you reach this place, it means there is no any file 
          // explorer app installed on your device
        }

View this thread

Upvotes: 0

Related Questions