我有一个应用程序,我希望用户从应用程序中的库中加载电影和照片。但该节目停止播放超过40 MB的电影。 1)如何获取所选的电影音量(大小)? 2)如何在不停止应用的情况下上传更大的电影
我试过这个,但返回0
long length = storeDirectory12.length();
length = length/1024;
Toast.makeText(getApplicationContext(), "Video size: "+length+" KB", Toast.LENGTH_LONG).show();
这是我的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedMedia = imageReturnedIntent.getData();
InputStream MediaStream = null;
try {
MediaStream = getContentResolver().openInputStream(selectedMedia);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/////////////////////save in directory
String root = Environment.getExternalStorageDirectory().toString();
String name = "";
File myDir = new File(root + "/tahlilgar");
myDir.mkdirs();
if (!myDir.exists()) {
myDir.mkdir();
}
name = UUID.randomUUID().toString();
File storeDirectory12 = new File(root + "/tahlilgar/" + name + ".mp4");
InputStream inputStream = getContentResolver().openInputStream(selectedMedia);
FileOutputStream fileOutputStream = new FileOutputStream(storeDirectory12);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
try {
for (int readNum; (readNum = inputStream.read(b)) != -1; ) {
bos.write(b, 0, readNum);
}
} catch (Exception e) {
}
byte[] bytes = bos.toByteArray();
fileByte = bytes;
try {
fileOutputStream.write(bos.toByteArray());
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
inputStream.close();
}
////////////////////////convert file to byte
// fileww= convertVideoToBytes(selectedMedia);
// img.setImageBitmap(yourSelectedImage);
}
}
File file = new File(filePath);
long fileSizeInBytes = file.length();
long fileSizeInKB = fileSizeInBytes / 1024;
long fileSizeInMB = fileSizeInKB / 1024;