Showing marker images now works (also with content:// URI).

content:// was required to get picture from the camera.
This commit is contained in:
Dennis Guse
2019-07-17 18:48:44 +02:00
parent 5e6a505a37
commit f792b9f59a
6 changed files with 47 additions and 42 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ android {
defaultConfig {
applicationId "com.google.android.maps.mytracks"
minSdkVersion 21
minSdkVersion 24
targetSdkVersion 28
testApplicationId "com.google.android.maps.mytracks.test"
@@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.fragments;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
@@ -183,8 +182,6 @@ public class MarkerDetailFragment extends Fragment {
updateWaypoint(false);
menu.findItem(R.id.marker_detail_view_photo).setVisible(waypoint.hasPhoto());
TrackIconUtils.setMenuIconColor(menu);
}
@@ -209,12 +206,6 @@ public class MarkerDetailFragment extends Fragment {
DeleteMarkerDialogFragment.newInstance(new long[] { markerId })
.show(getChildFragmentManager(), DeleteMarkerDialogFragment.DELETE_MARKER_DIALOG_TAG);
return true;
case R.id.marker_detail_view_photo:
intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(waypoint.getPhotoURI(), "image/*");
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
@@ -254,8 +245,9 @@ public class MarkerDetailFragment extends Fragment {
if (hasPhoto) {
handler.removeCallbacks(hideText);
Display defaultDisplay = getActivity().getWindowManager().getDefaultDisplay();
PhotoUtils.setImageView(photo, waypoint.getPhotoURI(), defaultDisplay.getWidth(), defaultDisplay.getHeight(), true);
PhotoUtils.setImageView(getActivity(), photo, waypoint.getPhotoURI(), defaultDisplay.getWidth(), defaultDisplay.getHeight(), true);
handler.postDelayed(hideText, HIDE_TEXT_DELAY);
}
@@ -16,8 +16,7 @@
package com.google.android.apps.mytracks.services.tasks;
import com.google.android.apps.mytracks.util.PhotoUtils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
@@ -27,7 +26,11 @@ import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import com.google.android.apps.mytracks.util.PhotoUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
/**
@@ -38,21 +41,23 @@ import java.lang.ref.WeakReference;
public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
private static final String TAG = BitmapLoader.class.getSimpleName();
// Use a WeakReference to ensure the ImageView can be garbage collected
private final WeakReference<ImageView> imageViewReference;
private final Context context;
private final Uri uri;
private final int targetWidth;
private final int targetHeight;
private final boolean fitWithin;
public BitmapLoader(
ImageView imageView, Uri uri, int targetWidth, int targetHeight, boolean fitWithin) {
// Use a WeakReference to ensure the ImageView can be garbage collected
public BitmapLoader(Context context, ImageView imageView, Uri uri, int targetWidth, int targetHeight, boolean fitWithin) {
imageViewReference = new WeakReference<>(imageView);
this.uri = uri;
this.context = context;
this.targetWidth = targetWidth;
this.targetHeight = targetHeight;
this.fitWithin = fitWithin;
this.uri = uri;
}
public Uri getUri() {
@@ -61,19 +66,26 @@ public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
// Get the image dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(uri.getPath(), options);
if (options.outWidth == 0 || options.outHeight == 0) {
InputStream inputStream = null;
try {
inputStream = context.getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
Log.e(TAG, "Could not load bitmap from uri.");
return null;
}
BitmapFactory.decodeStream(inputStream);
// Set imageWidth and imageHeight based on image rotation
int rotation = getRotation();
try {
inputStream = context.getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
return null;
}
int rotation = getRotation(inputStream);
int imageWidth;
int imageHeight;
@@ -88,13 +100,18 @@ public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
// Get a scaled down version of the image
options.inJustDecodeBounds = false;
options.inSampleSize = getInSampleSize(imageWidth, imageHeight);
options.inPurgeable = true;
Bitmap scaledBitmap = BitmapFactory.decodeFile(uri.getPath(), options);
try {
inputStream = context.getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
return null;
}
Bitmap scaledBitmap;
scaledBitmap = BitmapFactory.decodeStream(inputStream, null, options);
if (scaledBitmap == null) {
return null;
}
// Get the final bitmap after rotating the scaled down image
Bitmap bitmap;
if (rotation == 0 && fitWithin) {
@@ -140,9 +157,9 @@ public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
}
}
private int getRotation() {
private int getRotation(InputStream inputStream) {
try {
ExifInterface exifInterface = new ExifInterface(uri.getPath());
ExifInterface exifInterface = new ExifInterface(inputStream);
switch (exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) {
case ExifInterface.ORIENTATION_ROTATE_90:
@@ -16,8 +16,6 @@
package com.google.android.apps.mytracks.util;
import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
@@ -31,6 +29,8 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.maps.mytracks.R;
/**
* Utilities to display a list item.
*
@@ -79,7 +79,7 @@ public class ListItemUtils {
photo.getLayoutParams().height = photoHeight;
photo.setImageResource(android.R.color.transparent);
Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
PhotoUtils.setImageView(photo, Uri.parse(photoUrl), defaultDisplay.getWidth(), photoHeight, false);
PhotoUtils.setImageView(activity, photo, Uri.parse(photoUrl), defaultDisplay.getWidth(), photoHeight, false);
}
// Set icon
@@ -16,11 +16,12 @@
package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.services.tasks.BitmapLoader;
import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;
import com.google.android.apps.mytracks.services.tasks.BitmapLoader;
import java.lang.ref.WeakReference;
/**
@@ -34,7 +35,7 @@ public class PhotoUtils {
/**
* Sets an image view.
*
*
* @param imageView the image view
* @param uri the image uri
* @param targetWidth the target width
@@ -43,9 +44,9 @@ public class PhotoUtils {
* entire image (no cropping). False to fill the entire target area.
* (allow cropping).
*/
public static void setImageView(ImageView imageView, Uri uri, int targetWidth, int targetHeight, boolean fitWithin) {
public static void setImageView(Context context, ImageView imageView, Uri uri, int targetWidth, int targetHeight, boolean fitWithin) {
if (cancelBitmapLoader(imageView, uri)) {
BitmapLoader bitmapLoader = new BitmapLoader(imageView, uri, targetWidth, targetHeight, fitWithin);
BitmapLoader bitmapLoader = new BitmapLoader(context, imageView, uri, targetWidth, targetHeight, fitWithin);
WeakReference<BitmapLoader> bitmapLoaderReference = new WeakReference<>(bitmapLoader);
imageView.setTag(bitmapLoaderReference);
bitmapLoader.execute();
@@ -30,9 +30,4 @@ limitations under the License.
android:icon="@drawable/ic_menu_delete"
android:title="@string/menu_delete"
app:showAsAction="ifRoom" />
<item
android:id="@+id/marker_detail_view_photo"
android:icon="@drawable/ic_menu_view_photo"
android:title="@string/menu_view_photo"
app:showAsAction="ifRoom" />
</menu>