Upload files from phone intent

心已入冬 提交于 2019-12-11 06:07:46

问题


I've an android application where in an activity the user chooses a picture from the phone's memory to upload it to the server using the Action Pick intent

Intent i = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMAGE);

However; now the requirements has changed and the client wants to upload any file with any extension, what intent should I use in order to open the phone's memory (like the file manager) and choose whatever I want?

Now I'm using the Get content intent thanks to you guys:

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

but the problem is in the start activity for result

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 1) {
            Log.d("select pivture", "passed her");
            Uri selectedMediaUri = data.getData();

            String Fpath = selectedMediaUri.getPath();

            Log.d("Fpath", Fpath);
}

I get as follows:

/document/primary:Download/tool208.pdf

Which is not what I want, I want path like this one:

/external/images/media/6634

回答1:


Use below method:

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

UPDATE

   @Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data)  
   {  
          super.onActivityResult(requestCode, resultCode, data);  
         if(requestCode==PICKFILE_REQUEST_CODE){  
         }  
 }  


来源:https://stackoverflow.com/questions/41332040/upload-files-from-phone-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!