Support for video compress

This commit is contained in:
tom79 2019-09-21 16:29:33 +02:00
parent 099a43e29e
commit f2abcc44c5
4 changed files with 135 additions and 15 deletions

View File

@ -18,6 +18,7 @@ package app.fedilab.android.activities;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ContentResolver;
@ -1335,7 +1336,7 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
String mCurrentPhotoPath;
File photoFile = null;
Uri photoFileUri = null;
public static Uri photoFileUri = null;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
@ -1358,7 +1359,6 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
"app.fedilab.android.fileProvider",
photoFile);
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFileUri);
startActivityForResult(takePictureIntent, TAKE_PHOTO);
}
@ -1387,7 +1387,6 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean photo_editor = sharedpreferences.getBoolean(Helper.SET_PHOTO_EDITOR, true);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
picture_scrollview.setVisibility(View.VISIBLE);
if (data == null) {
Toasty.error(getApplicationContext(), getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
return;
@ -1430,7 +1429,6 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
}
} else if (requestCode == SEND_VOICE_MESSAGE && resultCode == RESULT_OK) {
Uri uri = Uri.fromFile(new File(getCacheDir() + "/fedilab_recorded_audio.wav"));
prepareUpload(TootActivity.this, uri, "fedilab_recorded_audio.wav", uploadReceiver);
} else if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) {
@ -1482,6 +1480,8 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
Toasty.error(activityWeakReference.get(), activityWeakReference.get().getString(R.string.toast_error), Toast.LENGTH_SHORT).show();
error = true;
}
activityWeakReference.get().findViewById(R.id.compression_loader).setVisibility(View.VISIBLE);
}
@ -1496,6 +1496,8 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
@Override
protected void onPostExecute(Void result) {
activityWeakReference.get().findViewById(R.id.compression_loader).setVisibility(View.GONE);
activityWeakReference.get().findViewById(R.id.picture_scrollview).setVisibility(View.VISIBLE);
if (!error) {
if (bs == null)
return;
@ -1666,7 +1668,6 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
Bundle extras = intent.getExtras();
if (extras != null && extras.getString("imageUri") != null) {
Uri imageUri = Uri.parse(extras.getString("imageUri"));
picture_scrollview.setVisibility(View.VISIBLE);
if (imageUri == null) {
Toasty.error(getApplicationContext(), getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
return;

View File

@ -25,6 +25,7 @@ import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -55,6 +56,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
@ -3420,15 +3422,15 @@ public class Helper {
ContentResolver cr = context.getContentResolver();
String mime = cr.getType(uriFile);
File file = new File(uriFile.getPath());
ByteArrayInputStream bs = null;
File destinationDirectory = new File(context.getCacheDir().getAbsolutePath()+"/compress");
if (!destinationDirectory.exists()) {
destinationDirectory.mkdirs();
}
File dir = new File(context.getCacheDir().getAbsolutePath()+"/compress");
if (!dir.exists()) dir.mkdirs();
String destinationDirectory = context.getCacheDir().getAbsolutePath()+"/compress/"+ file.getName();
InputStream resizedIS = null;
if (mime != null && mime.toLowerCase().contains("image")) {
if (mime == null || mime.toLowerCase().contains("image")) {
ExifInterface exif = null;
try (InputStream inputStream = context.getContentResolver().openInputStream(uriFile)) {
assert inputStream != null;
@ -3456,11 +3458,18 @@ public class Helper {
boolean compressed = sharedpreferences.getBoolean(Helper.SET_PICTURE_COMPRESSED, true);
if( compressed) {
try {
String filePath = SiliCompressor.with(context).compress(file.getAbsolutePath(), new File(destinationDirectory));
String filePath = SiliCompressor.with(context).compress(uriFile.toString(), destinationDirectory);
resizedIS = new FileInputStream(filePath);
} catch (IOException e) {
} catch (IOException|ArithmeticException e) {
e.printStackTrace();
}
if( resizedIS == null){
try {
resizedIS = context.getContentResolver().openInputStream(uriFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}else{
try {
resizedIS = context.getContentResolver().openInputStream(uriFile);
@ -3489,7 +3498,8 @@ public class Helper {
if( compressed) {
String filePath = null;
try {
filePath = SiliCompressor.with(context).compressVideo(file.getAbsolutePath(), destinationDirectory);
filePath = SiliCompressor.with(context).compressVideo(getRealPathFromURI(context,uriFile), context.getCacheDir().getAbsolutePath()+"/compress/");
} catch (URISyntaxException e) {
e.printStackTrace();
}
@ -3499,6 +3509,13 @@ public class Helper {
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if( resizedIS == null){
try {
resizedIS = context.getContentResolver().openInputStream(uriFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}else{
try {
resizedIS = context.getContentResolver().openInputStream(uriFile);
@ -3525,6 +3542,81 @@ public class Helper {
return bs;
}
public static String getRealPathFromURI(Context context, Uri uri) throws URISyntaxException {
String selection = null;
String[] selectionArgs = null;
// Uri is different in versions after KITKAT (Android 4.4), we need to
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
uri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("image".equals(type)) {
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
selection = "_id=?";
selectionArgs = new String[]{
split[1]
};
}
}
if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri)) {
return uri.getLastPathSegment();
}
String[] projection = {
MediaStore.Images.Media.DATA
};
Cursor cursor = null;
try {
cursor = context.getContentResolver()
.query(uri, projection, selection, selectionArgs, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
public static String getFileName(Context context, Uri uri) {
ContentResolver resolver = context.getContentResolver();
Cursor returnCursor =

View File

@ -17,9 +17,9 @@
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:aztec="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:fillViewport="true">
android:fillViewport="true"
app:ignore="NamespaceTypo">
<RelativeLayout
android:id="@+id/drawer_layout"
@ -169,6 +169,32 @@
android:padding="5dp"></LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:layout_width="match_parent"
android:visibility="gone"
android:id="@+id/compression_loader"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_vertical_margin"
android:layout_marginBottom="3dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="3dp"
android:orientation="horizontal">
<ProgressBar
android:layout_gravity="center"
android:layout_width="10dp"
android:layout_height="10dp"/>
<TextView
android:layout_gravity="center_vertical"
android:textColor="?colorAccent"
android:layout_marginStart="5dp"
android:textSize="12sp"
android:gravity="center_vertical"
android:text="@string/please_wait_compression"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

View File

@ -1223,4 +1223,5 @@
<string name="note_for_account">Notes for the account</string>
<string name="set_resize_picture_indication">Allow to compress large photos into smaller sized photos with very less or negligible loss in quality of the image.</string>
<string name="set_resize_video_indication">Allow to compress videos while maintaining their quality.</string>
<string name="please_wait_compression">The app is compressing the media, it can be quite long…</string>
</resources>