通过 content:// URI 从第三方应用程序(例如 WhatsApp)获取视频路径到我的应用程序
Getting Video path from third party app (e.g. WhatsApp) to my app via content:// URI
我正在尝试获取从第三方应用程序(例如 WhatsApp)到我的应用程序(正在 Marshmallow 上测试)的视频路径。当我从 WhatsApp 分享视频并与我的应用分享时,我得到的 URI 是这样的:
内容://com.whatsapp.provider.media/item/12
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
} else if (type.startsWith("image/")) {
} else if (type.startsWith("video/")) {
Uri videoUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
}
}
如何从上述URI获取视频文件的路径?
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
} else if (type.startsWith("image/")) {
} else if (type.startsWith("video/")) {
handleReceivedVideo(intent); // Handle video received from whatsapp URI
}
}
在handleReceivedVideo()中需要打开inputStream然后复制到文件中
void handleReceivedVideo(Intent intent) throws IOException {
Uri videoUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (videoUri != null) {
File file = new File(getCacheDir(), "video.mp4");
InputStream inputStream=getContentResolver().openInputStream(videoUri);
try {
OutputStream output = new FileOutputStream(file);
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} finally {
output.close();
}
} finally {
inputStream.close();
byte[] bytes =getFileFromPath(file);
}
}
}
getFileFromPath() 获取可以上传到服务器的字节数。
public static byte[] getFileFromPath(File file) {
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bytes;
}
我正在尝试获取从第三方应用程序(例如 WhatsApp)到我的应用程序(正在 Marshmallow 上测试)的视频路径。当我从 WhatsApp 分享视频并与我的应用分享时,我得到的 URI 是这样的:
内容://com.whatsapp.provider.media/item/12
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
} else if (type.startsWith("image/")) {
} else if (type.startsWith("video/")) {
Uri videoUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
}
}
如何从上述URI获取视频文件的路径?
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
} else if (type.startsWith("image/")) {
} else if (type.startsWith("video/")) {
handleReceivedVideo(intent); // Handle video received from whatsapp URI
}
}
在handleReceivedVideo()中需要打开inputStream然后复制到文件中
void handleReceivedVideo(Intent intent) throws IOException {
Uri videoUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (videoUri != null) {
File file = new File(getCacheDir(), "video.mp4");
InputStream inputStream=getContentResolver().openInputStream(videoUri);
try {
OutputStream output = new FileOutputStream(file);
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} finally {
output.close();
}
} finally {
inputStream.close();
byte[] bytes =getFileFromPath(file);
}
}
}
getFileFromPath() 获取可以上传到服务器的字节数。
public static byte[] getFileFromPath(File file) {
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bytes;
}