是否可以在Android中使用Intent将视频URL发送到Instagram应用程序? [英] Is it possible to send video url to instagram application using intent in android?

查看:40
本文介绍了是否可以在Android中使用Intent将视频URL发送到Instagram应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将视频URL发送到Instagram应用程序,但没有成功

string type="video/*";

String mediaPath="www.example.com/abcd.mp4";

Intent share = new Intent(Intent.ACTION_SEND);

        // Set the MIME type
        share.setType(type);

        // Create the URI from the media
        File media = new File(mediaPath);
        Uri uri = Uri.fromFile(media);

        // Add the URI to the Intent.
        share.putExtra(Intent.EXTRA_STREAM, uri);

        // Broadcast the Intent.
        startActivity(Intent.createChooser(share, "Share to"));

推荐答案

找了很久,终于找到了解决方案。首先,我们需要从url下载视频(如果视频未存储在本地存储中),然后应用以下代码。

/*单击按钮在Instagram上共享视频*/

btn_instagram.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isPackageInstalled("com.instagram.android")) {
                    createInstagramIntent(localuri);
                }else{
                    AlertDialog.Builder alert = new AlertDialog.Builder(ReflipActivity.this);
                    alert.setTitle("Warning");
                    alert.setMessage("Instagram App not found");
                    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    alert.show();
                }
            }
        });

//检查设备上是否安装了Instagram应用

private boolean isPackageInstalled(String packagename) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

//并最终触发Instagram的意图

private void createInstagramIntent(String filename){
        String settype = "video/*";
        String mediaPath =  filename;

        // Create the new Intent using the 'Send' action.
        Intent share = new Intent(Intent.ACTION_SEND);

        // Set the MIME type
        share.setType(settype);
        share.setPackage("com.instagram.android");

        // Create the URI from the media
        File media = new File(mediaPath);
        Uri uri = Uri.fromFile(media);

        // Add the URI to the Intent.
        share.putExtra(Intent.EXTRA_STREAM, uri);

        // Broadcast the Intent.
        startActivity(Intent.createChooser(share, "Share to"));
    }

这篇关于是否可以在Android中使用Intent将视频URL发送到Instagram应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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