简体   繁体   中英

Browse path on SD card via Intent

I'd like to know if there's a way to open a file browser (system or 3rd party like Astro) to a specific path. There's not much else to say here... pretty straight-forward question.

Sounds like ACTION_GET_CONTENT is what you want. See here . The relevant bits would be:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);

On my phone (which has astro already installed), this brings up an astro dialog for /sdcard. I'm not sure what this will do on a phone with no file browser installed. I'm also unsure about whether you are able to actually specify the starting path using this method. The docs make it sound like you can't specify a starting uri for ACTION_GET_CONTENT.

EDIT: I think I understand the question better now. I thought you were wanting a picker style browser to just get a file path from the user. If you want a full blown browser to handle your uri, then this worked for me:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///mnt/sdcard/Music"), "*/*");
startActivity(intent);

That will probably give you quite a long list of possible handlers, but I'd bet any file manager on the system would be in the list (astro certainly is).

I don't believe that any of the manufacturer provided File Browsers provide such a thing.

Though I don't see anything glaringly wrong with the theory of doing so. I imagine you are more likely to find a 3rd party file browser with this feature, but I've never come across any of those either.

You might look in to the Open Intents OI File Manager this concept seems right up their ally, if they don't already have this feature, I bet they might consider adding it if you get in contact with them.

Here i am going to show you that how to create a BROWSE button, which when you will click, it will open up the SDCARD, you will select a File and in result you will get the File Name and File path of the selected one:

// A button which you will hit**

 browse.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
         Intent intent = new Intent();
         intent.setAction(Intent.ACTION_PICK);
         Uri startDir = Uri.fromFile(new File("/sdcard"));
         startActivityForResult(intent, PICK_REQUEST_CODE);
        }

    });

//The function which will get the Resulted File Name and File Path

protected void onActivityResult(int requestCode, int resultCode, Intent intent)
 { 
   if (requestCode == PICK_REQUEST_CODE)
  {
  if (resultCode == RESULT_OK)
  {
     Uri uri = intent.getData();

         if (uri.getScheme().toString().compareTo("content")==0)
         {      
             Cursor cursor =getContentResolver().query(uri, null, null, null, null);
             if (cursor.moveToFirst())
             {
                 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);//Instead of "MediaStore.Images.Media.DATA" can be used "_data"
                 Uri filePathUri = Uri.parse(cursor.getString(column_index));
                 String file_name = filePathUri.getLastPathSegment().toString();
                 String file_path=filePathUri.getPath();
                 Toast.makeText(this,"File Name & PATH are:"+file_name+"\n"+file_path, Toast.LENGTH_LONG).show();
             }
         }
  }
   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM