如何将视频从 whatsapp 分享到我的应用程序 [英] How to share a video from whatsapp to my application

查看:66
本文介绍了如何将视频从 whatsapp 分享到我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to share a video from Whatsapp to other application

以下清单代码

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

推荐答案

我得到了解决方案

VdoPostActivity.java

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

这篇关于如何将视频从 whatsapp 分享到我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆