Skip to content

Commit

Permalink
fix(android): openPhotoGallery() crash selecting multiple files
Browse files Browse the repository at this point in the history
Fixes TIMOB-28193
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Oct 26, 2020
1 parent 590928e commit 0b1116f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -47,7 +46,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
Expand All @@ -58,7 +56,6 @@
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.view.Window;
Expand Down Expand Up @@ -956,7 +953,7 @@ public void onResult(Activity activity, int requestCode, int resultCode, Intent

// Fetch a URI to file selected. (Only applicable to single file selection.)
Uri uri = data.getData();
String path = uri.toString();
String path = (uri != null) ? uri.toString() : null;

// Handle multiple file selection, if enabled.
if (requestCode == PICK_IMAGE_MULTIPLE) {
Expand Down Expand Up @@ -1077,38 +1074,13 @@ protected static KrollDict createDictForImage(String path)

// Determine the mime type for the given file.
String mimeType = null;
boolean isPhoto = false;
try {
mimeType = contentResolver.getType(Uri.parse(path));
if ((mimeType != null) && mimeType.toLowerCase().startsWith("image")) {
isPhoto = true;
}
} catch (Exception ex) {
}

// Wrap the given file in a blob.
String[] parts = { path };
TiBlob imageData = null;
if (isPhoto && path.startsWith("content://com.google.android.apps.photos.contentprovider")) {
// This is an image file from the Google Photos cloud.
// We don't have direct file access to it. So, load it into memory.
ParcelFileDescriptor parcelFileDescriptor;
Bitmap image;
try {
parcelFileDescriptor = contentResolver.openFileDescriptor(Uri.parse(path), "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
if (image != null) {
imageData = TiBlob.blobFromImage(image);
}
} catch (Exception ex) {
}
}
if (imageData == null) {
// Have the blob wrap the file path or URL.
imageData = createImageData(parts, mimeType);
}
TiBlob imageData = createImageData(new String[] { path }, mimeType);

// Return a Titanium "CameraMediaItemType" dictionary which wraps the given file.
return createDictForImage(imageData, mimeType);
Expand Down
16 changes: 5 additions & 11 deletions android/titanium/src/java/org/appcelerator/titanium/TiBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,19 +564,13 @@ public String getNativePath()
@Kroll.getProperty
public TiFileProxy getFile()
{
if (data == null) {
return null;
}
if (this.type != TYPE_FILE) {
TiFileProxy fileProxy = null;
if (data instanceof TiBaseFile) {
fileProxy = new TiFileProxy((TiBaseFile) data);
} else if (data != null) {
Log.w(TAG, "getFile not supported for non-file blob types.");
return null;
} else if (!(data instanceof TiBaseFile)) {
Log.w(TAG,
"getFile unable to return value: underlying data is not file, rather " + data.getClass().getName());
return null;
} else {
return new TiFileProxy((TiBaseFile) data);
}
return fileProxy;
}

@Kroll.method
Expand Down

0 comments on commit 0b1116f

Please sign in to comment.