如何将视频从whatsapp共享到我的应用程序

时间:2018-07-26 04:12:36

标签: video share

如何将视频从Whatsapp共享到其他应用程序

遵循清单代码

<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 个答案:

答案 0 :(得分:1)

我有解决办法

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;
}