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 { defaultConfig {
applicationId "com.google.android.maps.mytracks" applicationId "com.google.android.maps.mytracks"
minSdkVersion 21 minSdkVersion 24
targetSdkVersion 28 targetSdkVersion 28
testApplicationId "com.google.android.maps.mytracks.test" testApplicationId "com.google.android.maps.mytracks.test"
@@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.fragments;
import android.content.Intent; import android.content.Intent;
import android.location.Location; import android.location.Location;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
@@ -183,8 +182,6 @@ public class MarkerDetailFragment extends Fragment {
updateWaypoint(false); updateWaypoint(false);
menu.findItem(R.id.marker_detail_view_photo).setVisible(waypoint.hasPhoto());
TrackIconUtils.setMenuIconColor(menu); TrackIconUtils.setMenuIconColor(menu);
} }
@@ -209,12 +206,6 @@ public class MarkerDetailFragment extends Fragment {
DeleteMarkerDialogFragment.newInstance(new long[] { markerId }) DeleteMarkerDialogFragment.newInstance(new long[] { markerId })
.show(getChildFragmentManager(), DeleteMarkerDialogFragment.DELETE_MARKER_DIALOG_TAG); .show(getChildFragmentManager(), DeleteMarkerDialogFragment.DELETE_MARKER_DIALOG_TAG);
return true; 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: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@@ -254,8 +245,9 @@ public class MarkerDetailFragment extends Fragment {
if (hasPhoto) { if (hasPhoto) {
handler.removeCallbacks(hideText); handler.removeCallbacks(hideText);
Display defaultDisplay = getActivity().getWindowManager().getDefaultDisplay(); 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); handler.postDelayed(hideText, HIDE_TEXT_DELAY);
} }
@@ -16,8 +16,7 @@
package com.google.android.apps.mytracks.services.tasks; 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.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Matrix; import android.graphics.Matrix;
@@ -27,7 +26,11 @@ import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
import com.google.android.apps.mytracks.util.PhotoUtils;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
/** /**
@@ -38,21 +41,23 @@ import java.lang.ref.WeakReference;
public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> { public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
private static final String TAG = BitmapLoader.class.getSimpleName(); 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 WeakReference<ImageView> imageViewReference;
private final Context context;
private final Uri uri; private final Uri uri;
private final int targetWidth; private final int targetWidth;
private final int targetHeight; private final int targetHeight;
private final boolean fitWithin; private final boolean fitWithin;
public BitmapLoader( public BitmapLoader(Context context, ImageView imageView, Uri uri, int targetWidth, int targetHeight, boolean fitWithin) {
ImageView imageView, Uri uri, int targetWidth, int targetHeight, boolean fitWithin) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<>(imageView); imageViewReference = new WeakReference<>(imageView);
this.uri = uri;
this.context = context;
this.targetWidth = targetWidth; this.targetWidth = targetWidth;
this.targetHeight = targetHeight; this.targetHeight = targetHeight;
this.fitWithin = fitWithin; this.fitWithin = fitWithin;
this.uri = uri;
} }
public Uri getUri() { public Uri getUri() {
@@ -61,19 +66,26 @@ public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
@Override @Override
protected Bitmap doInBackground(Void... params) { protected Bitmap doInBackground(Void... params) {
// Get the image dimensions // Get the image dimensions
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; 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; return null;
} }
BitmapFactory.decodeStream(inputStream);
// Set imageWidth and imageHeight based on image rotation // 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 imageWidth;
int imageHeight; int imageHeight;
@@ -88,9 +100,14 @@ public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
// Get a scaled down version of the image // Get a scaled down version of the image
options.inJustDecodeBounds = false; options.inJustDecodeBounds = false;
options.inSampleSize = getInSampleSize(imageWidth, imageHeight); 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) { if (scaledBitmap == null) {
return null; return null;
} }
@@ -140,9 +157,9 @@ public class BitmapLoader extends AsyncTask<Void, Void, Bitmap> {
} }
} }
private int getRotation() { private int getRotation(InputStream inputStream) {
try { try {
ExifInterface exifInterface = new ExifInterface(uri.getPath()); ExifInterface exifInterface = new ExifInterface(inputStream);
switch (exifInterface.getAttributeInt( switch (exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) { ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) {
case ExifInterface.ORIENTATION_ROTATE_90: case ExifInterface.ORIENTATION_ROTATE_90:
@@ -16,8 +16,6 @@
package com.google.android.apps.mytracks.util; package com.google.android.apps.mytracks.util;
import com.google.android.maps.mytracks.R;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
@@ -31,6 +29,8 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.google.android.maps.mytracks.R;
/** /**
* Utilities to display a list item. * Utilities to display a list item.
* *
@@ -79,7 +79,7 @@ public class ListItemUtils {
photo.getLayoutParams().height = photoHeight; photo.getLayoutParams().height = photoHeight;
photo.setImageResource(android.R.color.transparent); photo.setImageResource(android.R.color.transparent);
Display defaultDisplay = activity.getWindowManager().getDefaultDisplay(); 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 // Set icon
@@ -16,11 +16,12 @@
package com.google.android.apps.mytracks.util; 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.net.Uri;
import android.widget.ImageView; import android.widget.ImageView;
import com.google.android.apps.mytracks.services.tasks.BitmapLoader;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
/** /**
@@ -43,9 +44,9 @@ public class PhotoUtils {
* entire image (no cropping). False to fill the entire target area. * entire image (no cropping). False to fill the entire target area.
* (allow cropping). * (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)) { 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); WeakReference<BitmapLoader> bitmapLoaderReference = new WeakReference<>(bitmapLoader);
imageView.setTag(bitmapLoaderReference); imageView.setTag(bitmapLoaderReference);
bitmapLoader.execute(); bitmapLoader.execute();
@@ -30,9 +30,4 @@ limitations under the License.
android:icon="@drawable/ic_menu_delete" android:icon="@drawable/ic_menu_delete"
android:title="@string/menu_delete" android:title="@string/menu_delete"
app:showAsAction="ifRoom" /> 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> </menu>