java - 从 Snapchat 分享到我的应用程序

标签 java android share android-contentprovider snapchat

我无法弄清楚如何将视频从 SnapChat 分享到我的应用程序,而 WhatsApp 可以毫无问题地做到这一点,所以这是可能的。

由于 SnapChat 与 contentprovider 一起使用,我想我应该使用 android contentresolver 方法查询 uri。使用databaseUtils,我将光标转储到logcat,但它返回的只是显示名称和文件大小。我看不出如何获取实际文件。

大家帮帮我吧。我错过了什么?

最佳答案

有点晚了,但我设法解决了。这是解决方案。我在代码中添加了注释来解释我在做什么。

void handleSendVideo(Intent intent)
{
    // get title
    String title = intent.getExtras().getString(Intent.EXTRA_SUBJECT);

    // get the uri for the video
    mVideoUri = (Uri) intent.getExtras().get(Intent.EXTRA_STREAM);

    // check if it originates from snapchat
    if (mVideoUri.getAuthority().equals(UriHelper.SNAPCHAT_FILE_PROVIDER))
    {
        Intent mRequestFileIntent;
        ParcelFileDescriptor mInputPFD;
        mRequestFileIntent = new Intent(Intent.ACTION_PICK);
        mRequestFileIntent.setType("video/*");

        // Query the content resolver to get the name of the file. Beware that you will not find
        // the actual file here. You must read it from the fileDescriptor.
        Cursor fileDataCursor = getContentResolver().query(mVideoUri, null, null, null, null);
        String fileName = "";
        if (fileDataCursor != null)
        {
            fileDataCursor.moveToFirst();
            fileName = fileDataCursor.getString(0);
            fileDataCursor.close();
        }

        // something is wrong... return
        if (fileName.isEmpty())
        {
            return;
        }

        try {
            // open the file descriptor that belongs to the file given to us by snapchat.
            mInputPFD = getContentResolver().openFileDescriptor(mVideoUri, "r");

            // fetch the descriptor
            FileDescriptor fd = mInputPFD.getFileDescriptor();

            // create in input stream from descriptor
            InputStream inputStream = new FileInputStream(fd);

            // This is the file that will be created
            File targetFile = new File(Environment.getExternalStorageDirectory(), fileName);

            // Open a outputstream connected to the file
            FileOutputStream fileOutputStream = new FileOutputStream(targetFile);

            // Create a buffer with a size equal to what is available from the inputstream.
            // Note: we dont have to loop here because the file is available on the storage.
            byte[] buffer = new byte[inputStream.available()];

            // Read all data into the buffer
            inputStream.read(buffer);

            // Write the buffer to the outputstream
            fileOutputStream.write(buffer);

            // close all streams, our file is ready.
            inputStream.close();
            fileOutputStream.close();
            mInputPFD.close();

            // check if the new file exists.
            if (targetFile.exists())
            {
                // add the file to the android MediaProvider
                mVideoUri = addVideo(targetFile);
            }
            else
            {
                DialogMaker.showAlertMessage(this, "Could not read video", "unable to read video from SnapChat.", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });

                return;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        logger.i("videoUri: " + mVideoUri.toString());
        logger.i("videoPath: " + mVideoUri.getPath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public Uri addVideo(File videoFile) {
    ContentValues values = new ContentValues(3);
    values.put(MediaStore.Video.Media.TITLE, "My video title");
    values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
    values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath());
    return getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}

关于java - 从 Snapchat 分享到我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41638312/

相关文章:

java - 更改 JTable 中列的大小

java - struts2 Action 映射不正确

java - 前端和控制面板的不同 view.jsp

Android 无法从 Activity 访问 textView 组件

java - 从图库中选择图像时未获得正确的路径

ios - HealthKit实际上如何共享数据?

java - 在 Java 中调整 JButton 大小时遇到​​问题

java - 获取帐户 android 的身份验证 token 时显示对话框

javascript - Facebook 共享器链接适用于用户,但不适用于粉丝专页管理员

iOS 分享到 facebook/twitter 按钮图片