Использование SiliCompressor
Вот пример кода:
Класс сжатия видео
class VideoCompressAsyncTask extends AsyncTask<String, String, String> {
Context mContext;
String thumbnail;
public VideoCompressAsyncTask(Context context, String thumbnail){
mContext = context;
this.thumbnail = thumbnail;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Uploading Video...");
pd.show();
}
@Override
protected String doInBackground(String... paths) {
String filePath = null;
try {
filePath = SiliCompressor.with(mContext).compressVideo(paths[0], paths[1]);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return filePath;
}
@Override
protected void onPostExecute(String compressedFilePath) {
super.onPostExecute(compressedFilePath);
UploadMedia(compressedFilePath, thumbnail,"video");
Log.i("Silicompressor", "Path: "+compressedFilePath);
}
}
и в onActivityResult
вы называете этот класс
if(requestCode == SELECT_VIDEO && resultCode == RESULT_OK)
{
Uri uri = data.getData();
String[] projection = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if(cursor!=null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + "/media/videos");
if (f.mkdirs() || f.isDirectory())
//compress and output new video specs
new VideoCompressAsyncTask(this,getThumbnailPathForLocalFile(this,uri)).execute(cursor.getString(column_index), f.getPath());
cursor.close();
}else
Toast.makeText(ChatScreen.this, "No Video Found", Toast.LENGTH_SHORT).show();
}
также вы можете получить миниатюру видео с помощью этого метода
public static String getThumbnailPathForLocalFile(ChatScreen context, Uri fileUri ) {
long fileId = getFileId(context, fileUri);
Log.e("ID",fileId+"");
MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
fileId, MediaStore.Video.Thumbnails.MICRO_KIND, null);
Cursor thumbCursor = null;
try {
thumbCursor = context.managedQuery(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + " = " + fileId, null, null);
if (thumbCursor.moveToFirst()) {
String thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
Log.e("ThumbPath",thumbPath);
return thumbPath;
}
} finally {
}
return null;
}