Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-11183] Android: Intent com.android.camera.action.CROP not returning data

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2014-05-28T16:09:12.000+0000
Affected Version/sRelease 2.1.2, Release 2.1.3, Release 3.0.0
Fix Version/sRelease 3.1.0, 2013 Sprint 03 API, 2013 Sprint 03
ComponentsAndroid
Labelsn/a
ReporterAndré Perazzi
AssigneeVivekr
Created2012-09-20T09:51:48.000+0000
Updated2016-05-20T17:56:38.000+0000

Description

When starting an activity for an image crop intent's result, I cannot reach the data returned. If i put the extra property ('return-data',true) into it, I can get a the text 'Android.Graphics.Bitmap@' after calling intent.getStringExtra("data"), but the data property of the returned intent is still null. I'm attaching a code showing the problem. It's almost impossible to let the user crop an image on Android, since the intent is not working and there are too many limitations when dealing with images on Android (such as pinch, scroll, etc).
var curWin = Ti.UI.createWindow({
	backgroundColor : '#ffffff',
	//navBarHidden:true, // IF THIS IS ENABLED THE startActivityForResult NOT WORK
	exitOnClose : true
});

var myView = Ti.UI.createView();
var curBtn = Ti.UI.createButton({
	color : '#000000',
	title : 'Start Crop',
	height : 'auto',
	width : 'auto'
});
myView.add(curBtn);

curBtn.addEventListener('click', function(e) {
	try {
		Titanium.Media.openPhotoGallery({
			success : function(event) {
				var intent = Ti.Android.createIntent({
					action : "com.android.camera.action.CROP",
					data : event.media.nativePath,
					type : 'image/*'
				});
				intent.putExtra("outputX", 200);
				intent.putExtra("outputY", 200);
				intent.putExtra("aspectX", 1);
				intent.putExtra("aspectY", 1);
				intent.putExtra("scale", true);
				intent.putExtra("return-data", true);
				var activity = Ti.Android.currentActivity;
				activity.startActivityForResult(intent, function(param) {
					var iname = Ti.Android.EXTRA_STREAM;
					if (param.resultCode == Ti.Android.RESULT_OK) {
						alert('ok');
						if (param.intent) {
							// Daniel: The following calls return errors. Uncomment to clarify problem description.
							//alert(JSON.stringify(param.intent.getStringExtra("data")));
							//alert(JSON.stringify(param.intent.getBlobExtra("data")));
						}
                     //vivekr : param.intent.getBlobExtra("data") works
                     var imageData = param.intent.getBlobExtra("data"); 
						var image = Ti.UI.createImageView({
							// Daniel: Problem description uses this, but the function doesn't exist: 
							// param.intent.getExtra("return-data")
							// Andre: In my description, I meant that if I put the "intent.putExtra("return-data", true);"
							// The result seems to come with data. If you remove this line, the result will be null
							// vivekr: This doesnt work - image : param.intent.data 
                                                        
                            image : imageData,
							width : 300,
							height : 300,
							top : 0,
							left : 0,
							backgroundColor : '#FFF'
						});
						var newWin = Ti.UI.createWindow({
							backgroundColor : '#000',
							//navBarHidden:true,
							exitOnClose : true
						});
						newWin.add(image);
						newWin.open();
					}
				});
			},
			error : function(error) {
			},
			cancel : function() {
			}
		});
	} catch(e) {
	}
});
curWin.add(myView);
curWin.open();

Attachments

FileDateSize
Code.js2012-09-20T09:51:49.000+00002298

Comments

  1. Daniel Sefton 2012-09-21

    André: If I understand correctly, you seem to have two separate problems with this code: one that startActivityForResult is not fired when navBarHidden is set to true, and one that the data result is null. Is that correct? From the comment you added in the code, did using "param.intent.data" fix the null problem?
  2. André Perazzi 2012-09-21

    Yes for the 1st question, but the navBarHidden:true is a known issue. That's not a problem for me. My real problem is that the data result is null, no matter what I do. By adding "intent.putExtra("return-data", true);" the problem is not fixed, but I can get the result 'Android.Graphics.Bitmap@' by calling "param.intent.getStringExtra("data"))". Note that this data is a String, and not the image (object) itself. I can't find a way how to get the data object returned.
  3. Marcio Cunha 2012-11-30

    This bug makes almost impossible to crop an image on Androd, since Android has a lot of limitations such as pinch zoom, scrolling images in a scrollview (You can't put an image view in a scrollview when the image is actually bigger than the scrollview). Hope this bug gets fixed soon.
  4. Vivekr 2013-01-26

    Working on it
  5. Vishal Duggal 2013-01-29

    Pull pending https://github.com/appcelerator/titanium_mobile/pull/3814
  6. Paras Mishra 2013-03-04

    Image is getting cropped as expected. Verified on : SDK version: 3.1.0.v20130303194855 CLI version : 3.0.24 Android : 3.2
  7. Vittorio Sorbera 2016-05-20

    I used this code on Ti SDK 5.2.2.GA:
       var win = Ti.UI.createWindow({
       	backgroundColor : '#ffffff',
       	exitOnClose : true
       });
       var myView = Ti.UI.createView();
       var curBtn = Ti.UI.createButton({
       	color : '#000000',
       	title : 'Select Photo',
       	height : 'auto',
       	width : 'auto',
       	top : 10
       });
       var imageView = Ti.UI.createImageView({
       	width : 200,
       	height : 200,
       	backgroundColor : '#ccc'
       });
       myView.add(curBtn);
       myView.add(imageView);
       
       curBtn.addEventListener('click', function(e) {
       	if(!Titanium.Filesystem.hasStoragePermissions()) {
       		if (Ti.Platform.Android.API_LEVEL >= 23) {
       			Titanium.Filesystem.requestStoragePermissions(function(e) {
       				if (e.success) {
       					openPhotoGallery();
       				} else {
       					alert("Need permissions");
       				};
       			});
       		} else {
       			openPhotoGallery();
       		};
       	}else{
       		openPhotoGallery();
       	};
       });
       
       function openPhotoGallery(){
       	Ti.Media.openPhotoGallery({
       		success : function(event) {				
       			var intent = Ti.Android.createIntent({
       				action : "com.android.camera.action.CROP",
       				data : event.media.nativePath,
       				type : 'image/*'
       			});
       			intent.putExtra("scale", true);
       			intent.putExtra("return-data", true);
       			var activity = win.getActivity();
       			activity.startActivityForResult(intent, function(param) {					
       				if (param.resultCode == Ti.Android.RESULT_OK) {
       					if (param.intent) {
       						var imageData = param.intent.getBlobExtra("data");
       						imageView.image = imageData;
       					}
       				}
       			});
       		},
       		error : function(error) {
       			// error...
       		},
       		cancel : function() {
       			// cancel...
       		}
       	});
       };
       
       win.add(myView);
       win.open();
       
       
    It seems to work for Android versions <6. But on Android 6 (API = 23) I get this error:
       05-20 19:19:27.214: E/PhotosUEHandler(570): Uncaught exception in background thread Thread[BackgroundTask #1,1,main]
       05-20 19:19:27.214: E/PhotosUEHandler(570): java.lang.IllegalArgumentException: mediaStoreUri must be a MediaStore Uri
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at p.a(PG:5135)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at gqa.a(PG:230)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at got.b(PG:144)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at got.a(PG:2085)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at fgo.a(PG:44)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at gob.a(PG:49)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at mdb.e(PG:290)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at mdb.f(PG:304)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at mdr.run(PG:90)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at pci.run(PG:85)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       05-20 19:19:27.214: E/PhotosUEHandler(570): 	at java.lang.Thread.run(Thread.java:818)
       05-20 19:19:27.236: E/AndroidRuntime(570): FATAL EXCEPTION: BackgroundTask #1
       05-20 19:19:27.236: E/AndroidRuntime(570): Process: com.google.android.apps.photos, PID: 570
       05-20 19:19:27.236: E/AndroidRuntime(570): java.lang.IllegalArgumentException: mediaStoreUri must be a MediaStore Uri
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at p.a(PG:5135)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at gqa.a(PG:230)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at got.b(PG:144)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at got.a(PG:2085)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at fgo.a(PG:44)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at gob.a(PG:49)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at mdb.e(PG:290)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at mdb.f(PG:304)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at mdr.run(PG:90)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at pci.run(PG:85)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       05-20 19:19:27.236: E/AndroidRuntime(570): 	at java.lang.Thread.run(Thread.java:818)
       

JSON Source