How to share a video from whatsapp to my applicati

2019-08-23 04:27发布

问题:

How to share a video from Whatsapp to other application

Following Manifest Code

<activity
            android:name=".Vdo_PostActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Design.NoActionBar" >

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <data android:mimeType="video/*" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </activity>

回答1:

i got solution

VdoPostActivity.java

oncreate(Bundle b){
    Intent receivedIntent = getIntent();
        String receivedAction = receivedIntent.getAction();
        String receivedType = receivedIntent.getType();

...

    if(receivedAction!=null){
                if(receivedAction.equals(Intent.ACTION_SEND)){
                    //content is being shared
                    if(receivedType.startsWith("text/")){
                        //handle sent text
                    }
                    else if(receivedType.startsWith("video/")){
                        //handle sent image
                        Uri receivedUri = (Uri)receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
                       String  filename=getFileName(receivedUri);


                        if (receivedUri != null) {

                            String datas = null;
                            try {
                               // uri = getFilePath(this, receivedUri);
                              String uri=getFilePathFromUri(this,receivedUri);

                                System.out.println("Uri-------------------------------->"+uri);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

}



 private String getFilePathFromUri(Context context, Uri contentUri) {
    FileOutputStream out = null;
    String fileName = getFileNames(contentUri);
    if (!TextUtils.isEmpty(fileName)) {
        File copyFile = new File(TEMP_DIR_PATH + File.separator + fileName);

        try {
            File folder = new File(TEMP_DIR_PATH);
            if (!folder.exists())
                folder.mkdirs();

            File mypath = new File(folder, "VID_" + System.currentTimeMillis() + ".mp4");

            out = new FileOutputStream(mypath);
        }
        catch (Exception e){
            System.out.println("Error Mobe--------------------------------->"+e);
        }

        finally {
            try {
                if (out != null)
                    out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        copy(context, contentUri, copyFile);
        return copyFile.getAbsolutePath();
    }
    return null;
}

private void copy(Context context, Uri srcUri, File dstFile) {


    try {
        InputStream inputStream = context.getContentResolver().openInputStream(srcUri);
        if (inputStream == null) return;
        OutputStream outputStream = new FileOutputStream(dstFile);
        IOUtils.copy(inputStream, outputStream);
        inputStream.close();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private String getFileNames(Uri uri) {

    if (uri == null) return null;
    String fileName = null;
    String path = uri.getPath();
    int cut = path.lastIndexOf('/');
    if (cut != -1) {
        fileName = path.substring(cut + 1);
    }
    return fileName;
}


标签: video share