Get Path from another app (WhatsApp)

后端 未结 7 1200
萌比男神i
萌比男神i 2020-12-13 22:49

I\'m not getting Path from image or video from uri that I receive from whatsApp.

Uri comes like this: content://com.whatsapp.provider.media/item/16695

Media

7条回答
  •  情书的邮戳
    2020-12-13 23:35

    It works for me for opening small text file... I didn't try in other file

    protected void viewhelper(Intent intent) {
        Uri a = intent.getData();
        if (!a.toString().startsWith("content:")) {
            return;
        }
        //Ok Let's do it
        String content = readUri(a);
        //do something with this content
    }
    

    here is the readUri(Uri uri) method

    private String readUri(Uri uri) {
        InputStream inputStream = null;
        try {
            inputStream = getContentResolver().openInputStream(uri);
            if (inputStream != null) {
                byte[] buffer = new byte[1024];
                int result;
                String content = "";
                while ((result = inputStream.read(buffer)) != -1) {
                    content = content.concat(new String(buffer, 0, result));
                }
                return content;
            }
        } catch (IOException e) {
            Log.e("receiver", "IOException when reading uri", e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    Log.e("receiver", "IOException when closing stream", e);
                }
            }
        }
        return null;
    }
    

    I got it from this repository https://github.com/zhutq/android-file-provider-demo/blob/master/FileReceiver/app/src/main/java/com/demo/filereceiver/MainActivity.java
    I modified some code so that it work.

    Manifest file:

        
            
                
                
                
            
        
    

    You need to add

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /*
         *    Your OnCreate
         */
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
    
        //VIEW"
        if (Intent.ACTION_VIEW.equals(action) && type != null) {
            viewhelper(intent); // Handle text being sent
        }
      }
    

提交回复
热议问题