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;
}