video - 如何将视频从 whatsapp 分享到我的应用程序
问题描述
如何将视频从 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>
解决方案
我有解决方案
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;
}
推荐阅读
- javascript - Javascript 在 Google Chrome 控制台中工作,但在网站上不工作
- javascript - 在 vue.js 中将数组列为表行的正确格式
- pdf - 如何计算自动字体大小的 PDF 字段的实际字体大小?
- fortran - gfortran中的并行编程,奇怪吗?结果是 hello 程序
- pandas - 如何打印所有列的列表?
- javascript - 我可以使用 2D 数组在 JS 中创建井字游戏 AI 吗?
- html - 如何使非空白选择器更具选择性
- javascript - 如何防止图像在移动应用中滚动时移动
- google-cloud-platform - 存储桶未显示在 Google 存储列表中,并且每个命令都拒绝访问
- r - 如何根据字符串而不是值更改闪亮仪表板中信息框的颜色?