3

I did a major update of my question because some misunderstandings.

So, I have a notification. And when I click that notification, I want the file explorer app (third party app of course) opens. No matter whuch app is, if there's more than on file explorers, it should prompt the "open with" - and then open /sdcard/folder (if possible)

My notification it's here on Listar.class

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_menu_save, msg, System.currentTimeMillis());
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Open.class), 0);
        notification.setLatestEventInfo(this, filename+" downloaded", "Click to open folder", contentIntent);
        manager.notify(1, notification);

and my Open class it's here:

public class Open extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try{
        Intent intent = new Intent("org.openintents.action.PICK_FILE");
        startActivityForResult(intent, 1);
    }

    catch(Exception e)
    {
        Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        Log.v("EX",e.toString());
    }
    }

}

This opens oi file manager (without "open with" - i didnt chose default app) and to /sdcard, that's why i need your help.

Tiago
  • 1,116
  • 5
  • 25
  • 39
  • I don't know that there is a standard Android supported way to open a file explorer. In other words I do not believe Android provides a file explorer activity/app that a user can see and interact with. – rf43 Jul 21 '11 at 20:14
  • @DDoSAttack is right, you have to write your own file IO for your app, or if you just want a file explorer, download a third party app like Astro from the app store. – Noah Jul 21 '11 at 20:27
  • @sqrfv I know that depends from a third party app... I just need to know that, if it exists, how to call it! – Tiago Jul 21 '11 at 20:27
  • There is no such thing as intent.setData(Uri.parse("file:///sdcard")); use http://developer.android.com/guide/topics/data/data-storage.html#filesExternal – rf43 Jul 21 '11 at 20:56
  • So what can I do to get a "open with" prompt and open a specified folder? Any idea ? – Tiago Jul 21 '11 at 21:07
  • All of that will be here: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal esp look at the subsection "Accessing files on external storage" – rf43 Jul 21 '11 at 23:56

3 Answers3

3
Intent toLaunch = new Intent();
toLaunch.setAction(android.content.Intent.ACTION_VIEW);
toLaunch.setDataAndType(Uri.fromFile(new File("file_path")), MimeTypeMap.getSingleton().getMimeTypeFromExtension("jpeg"));  // you can also change jpeg to other types

and instead of:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Open.class), 0);

write:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, toLaunch , 0);
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
1

Notifications are designed to launch activities. In your case, you are launching Listar.class. That class should do the action you require, such as opening / displaying a file.

http://developer.android.com/reference/android/app/PendingIntent.html should describe the purpose of the PendingIntent that you have constructed.

Noah
  • 1,966
  • 1
  • 14
  • 29
  • Yes, I know, my primary question is: in my case I'm calling the same activity. Let's say I have other activity "Open". How is the code to open the file explorer to a specified location ? – Tiago Jul 21 '11 at 19:45
  • I believe that the default behavior is to open a new instance and leave the one running in the background. The new instance would then have the file location. If you just want to update the app if its already running see: http://stackoverflow.com/questions/2424488/android-new-intent-starts-new-instance-with-androidlaunchmode-singletop for an example. – Noah Jul 21 '11 at 19:53
  • That just shows how to start activities, and not how to start activity as I need... updated question. – Tiago Jul 21 '11 at 20:08
1

If you wanting to find out if a specific intent exists (e.g. the package is installed) you can use something like this...

/**
 * Checks the system to see if the passed intent package exists.
 * 
 * @param pIntent
 *            intent to be checked
 * @return true if the package exists
 */
private final boolean checkIfPackageExists( final Intent pIntent ) {
    // Build package manager
    final PackageManager packageManager = this.getPackageManager();
    final List<ResolveInfo> list = packageManager.queryIntentActivities( pIntent, PackageManager.MATCH_DEFAULT_ONLY );

    return list.size() > 0;
}
rf43
  • 4,385
  • 3
  • 23
  • 28
  • @Tiago if you want to open a third party app you should check to make sure that someone is going to receive your intent. That is where the code I posted comes in. – rf43 Jul 21 '11 at 20:58
  • Yes, that's what I was thinking right now when I was getting some exceptions :) Thanks! – Tiago Jul 21 '11 at 21:03
  • However I just can check one by one? There's no intent tag for folder navigation to get the open with prompt ? – Tiago Jul 21 '11 at 21:31