From 055a39cd5035726e2871dfc6aec70c1f2acd259b Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Sun, 15 Oct 2017 01:55:54 +0200 Subject: [PATCH] display progress indicator while loading image --- .../java/de/trac/spherical/MainActivity.java | 76 +++++++++++-------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/de/trac/spherical/MainActivity.java b/app/src/main/java/de/trac/spherical/MainActivity.java index 94f2d56..c000345 100644 --- a/app/src/main/java/de/trac/spherical/MainActivity.java +++ b/app/src/main/java/de/trac/spherical/MainActivity.java @@ -6,6 +6,7 @@ import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; +import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; @@ -165,41 +166,52 @@ public class MainActivity extends AppCompatActivity { if (intent == null) { throw new AssertionError("Intent is null!"); } - String type = intent.getType(); - if (type != null) { - Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); - if (imageUri == null) { - Toast.makeText(this, R.string.toast_file_not_found, Toast.LENGTH_SHORT).show(); - return; - } - - Log.d(TAG, "START LOADING BITMAP"); - try { - bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); - metadata = PhotoSphereParser.parse(getContentResolver().openInputStream(imageUri)); - } catch (IOException e) { - e.printStackTrace(); - } - Log.d(TAG, "FINISHED LOADING BITMAP"); - - switch (type) { - case MIME_PHOTO_SPHERE: - displayPhotoSphere(); - break; - - default: - if (metadata != null) { - displayPhotoSphere(); - } else { - displayFlatImage(); - } - break; - } - - } else { + final String type = intent.getType(); + if (type == null) { Toast.makeText(this, "TODO: Figure out what to do :D", Toast.LENGTH_SHORT).show(); + return; } + + final Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); + if (imageUri == null) { + Toast.makeText(this, R.string.toast_file_not_found, Toast.LENGTH_SHORT).show(); + return; + } + + // process image asynchronous. + new AsyncTask() { + @Override + protected Void doInBackground(Uri... params) { + Uri uri = params[0]; + + try { + bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); + metadata = PhotoSphereParser.parse(getContentResolver().openInputStream(uri)); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + @Override + protected void onPostExecute(Void result) { + switch (type) { + case MIME_PHOTO_SPHERE: + displayPhotoSphere(); + break; + + default: + if (metadata != null) { + displayPhotoSphere(); + } else { + displayFlatImage(); + } + break; + } + } + }.execute(imageUri); } /**