Fixes issue #288 - Crash when sharing a .mp4 file

This commit is contained in:
stom79 2018-01-30 08:59:39 +01:00
parent 852c5f268f
commit ec61cd2b0c
1 changed files with 30 additions and 19 deletions

View File

@ -18,6 +18,7 @@ import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -747,32 +748,42 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) { if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
picture_scrollview.setVisibility(View.VISIBLE); picture_scrollview.setVisibility(View.VISIBLE);
if (data == null) { if (data == null || data.getData() == null) {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
return; return;
} }
try { try {
//noinspection ConstantConditions
InputStream inputStream = getContentResolver().openInputStream(data.getData());
File photoFiletmp;
try {
photoFiletmp = createImageFile(false);
OutputStream output = new FileOutputStream(photoFiletmp);
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
assert inputStream != null; ContentResolver cr = getContentResolver();
while ((read = inputStream.read(buffer)) != -1) { String mime = cr.getType(data.getData());
output.write(buffer, 0, read); if(mime != null && mime.toLowerCase().contains("video")) {
InputStream inputStream = getContentResolver().openInputStream(data.getData());
new HttpsConnection(TootActivity.this).upload(inputStream, TootActivity.this);
} else if(mime != null && mime.toLowerCase().contains("image")) {
//noinspection ConstantConditions
InputStream inputStream = getContentResolver().openInputStream(data.getData());
File photoFiletmp;
try {
photoFiletmp = createImageFile(false);
OutputStream output = new FileOutputStream(photoFiletmp);
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
assert inputStream != null;
while ((read = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
new asyncPicture(TootActivity.this, photoFiletmp).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} finally {
output.close();
} }
output.flush(); } catch (IOException e) {
new asyncPicture(TootActivity.this, photoFiletmp).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); e.printStackTrace();
} finally {
output.close();
} }
} catch (IOException e) { }else {
e.printStackTrace(); Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();