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 f8c8172 commit 29b4116
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 44 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.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -62,7 +61,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 @@ -967,11 +965,10 @@ public void onResult(Activity activity, int requestCode, int resultCode, Intent
return;
}
Log.d(TAG, "OnResult called: " + resultCode, Log.DEBUG_MODE);
Uri uri = data.getData();
String path = null;
if (data != null) {
path = uri.toString();
}
// Fetch a URI to file selected. (Only applicable to single file selection.)
Uri uri = (data != null) ? data.getData() : null;
String path = (uri != null) ? uri.toString() : null;

//Starting with Android-L, backing out of the gallery no longer returns cancel code, but with
//an ok code and a null data.
if (resultCode == Activity.RESULT_CANCELED || (Build.VERSION.SDK_INT >= 20 && data == null)) {
Expand Down Expand Up @@ -1103,38 +1100,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 29b4116

Please sign in to comment.