0
public class AddImage extends AppCompatActivity {
   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_account);
}

   @Override
   protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
   
       if(requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null){
           uriSecurityQuestion = data.getData();
           ivSecurityQuestions.setImageURI(uriSecurityQuestion);
           ivSecurityQuestions.setVisibility(View.VISIBLE);
       }
   }
}

I send a request to add a .png file, then on the intent return, the onActivityResult is called with the intent data (containing the filepath to the .png selected)

From the Intent data, I don't get a path that is /storage/emulated/0/, but rather I get something like /document/msf:49 . How can I get the absolute filesystem path?

data.getData().getPath() doesn't seem to give the path I want.

I want to use this path to create a java.io.File object and push it to my google drive api service. When I manually type the path to the file myself, it does seem to upload the document so my only roadblock is getting the correct filepath.

  • If you are using Retrofit or OkHttp to work with the Google Drive REST API, [this answer](https://stackoverflow.com/a/56308643/115145) (and the resources that it links to) shows up to use the `Uri` itself for content upload. – CommonsWare Jan 02 '21 at 21:31
  • Neither...using the Google Api. I simply return from the pick image intent, and that uri isn't a file location. Somehow I want to convert that uri into a valid filepath on my phone. –  Jan 02 '21 at 21:36
  • Then perhaps you should switch. Or, see if it has an option to work with an `InputStream` or `Uri` or `FileDescriptor`. Or, copy the content to a file that you control, then use the resulting file. "Somehow I want to convert that uri into a valid filepath on my phone" -- sorry, but that is not an option. As the linked-to duplicates point out, the user is not necessarily picking something from a filesystem path that you can access. That is especially true on Android 11 and higher. – CommonsWare Jan 02 '21 at 21:37
  • Hmm so how can I work around getting an absolute path from the intent? I've been going in circles trying to get a way. –  Jan 02 '21 at 21:38
  • "Hmm so how can I work around getting an absolute path from the intent?" -- I covered this in my previous comment. You could perhaps switch to using Retrofit. Or, see if your chosen API has an option to work with an `InputStream` or `Uri` or `FileDescriptor`. Or, copy the content to a file that you control, then use the resulting file. – CommonsWare Jan 02 '21 at 21:40
  • I do have access to the inputstream...so I could create a tmp file and use that I guess. Not ideal however. –  Jan 02 '21 at 21:42

0 Answers0