0

When I do "share image" from WhatsApp and share it with my app, I get URI something like this:

content://com.whatsapp.provider.media/item/61025 but I can't able to get a file path from a Uri.

  • 2
    and you cannot get a file path... what do you need it for? what you need is `InputStream`, not a "file path", right? – pskink Nov 03 '17 at 15:51
  • If you're trying to get the image, then [this answer](https://stackoverflow.com/a/46481742/1219389) should help you out – PPartisan Nov 03 '17 at 15:53
  • I want the file path l@pskink –  Nov 03 '17 at 16:06
  • It's not posible to get file path from a uri from a Content Provider. However, if it's file uri `file:///` or content uri `content://com.externalstorage.documents/...` you can get absolute file path from uri. As pskink pointed out, get an `InputStream` and create a temprorary file using that `InputStream`. – Thracian Nov 03 '17 at 16:06
  • @FatihOzcan yes using contentResolver I got the file path from google photos URI but it is not working with WhatsApp content provider –  Nov 03 '17 at 16:12
  • ok so what do you need that path for? what do you want to do with that file? – pskink Nov 03 '17 at 16:12
  • @pskink I want to display that file and upload using multipart/form-data –  Nov 03 '17 at 16:13
  • so what you need is `ContentResolver#query` and `ContentResolver#openInputStream` methods - again you cannot get "file path" from your `Uri` – pskink Nov 03 '17 at 16:15
  • @pskink I have already use the ContentResolver#query but can't get the file path –  Nov 03 '17 at 16:19

2 Answers2

0

Following the Documentation , i receive the image intent through :

void onCreate (Bundle savedInstanceState) {
    ...
    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    } 
}

ContentResolver wasn't working in this case as "_data" field was returning null. So i found another way to get a file from content URI.

In handleSendImage() you need to open inputStream and then copy it into a file.

void handleSendImage(Intent intent) throws IOException {
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (imageUri != null) {
      File file = new File(getCacheDir(), "image");
      InputStream inputStream=getContentResolver().openInputStream(imageUri);
      try {

        OutputStream output = new FileOutputStream(file);
        try {
          byte[] buffer = new byte[4 * 1024]; // or other buffer size
          int read;

          while ((read = inputStream.read(buffer)) != -1) {
            output.write(buffer, 0, read);
          }

          output.flush();
        } finally {
          output.close();
        }
      } finally {
        inputStream.close();
        byte[] bytes =getFileFromPath(file);
        //Upload Bytes.
      }
    }
  }

getFileFromPath() gets you the bytes which you can upload on your server.

  public static byte[] getFileFromPath(File file) {
    int size = (int) file.length();
    byte[] bytes = new byte[size];
    try {
      BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
      buf.read(bytes, 0, bytes.length);
      buf.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return bytes;
  }
Anonymous
  • 2,184
  • 15
  • 23
0

You can use

IputStream is = getContentResolver().OpenInputStream("whatsapp Uri");

Check this answer

Miftakhul Arzak
  • 1,166
  • 1
  • 12
  • 31