gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:51 28 4
gpt4 key购买 nike

我无法弄清楚如何将视频从 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com