My app allows the user to press a button, it opens the camera, they can take a photo and it will show up in an imageview. If the user presses back or cancel while the camera
Adding this first conditional should work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != RESULT_CANCELED){
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
If the user cancel the request, the data will be returned as NULL
. The thread will throw a nullPointerException
when you call data.getExtras().get("data");
. I think you just need to add a conditional to check if the data returned is null.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
if (data != null)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
For Kotlin Users don't forget to add ? in data: Intent?
like
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}
I recommend using this:
Retrieve the father Intent
.
Intent intentParent = getIntent();
Convey the message directly.
setResult(RESULT_OK, intentParent);
This prevents the loss of its activity which would generate a null data error.
For Kotlin Users
You just need to add ? with Intent in onActivityResult
as the data can be null
if user cancels the transaction or anything goes wrong. So we need to define data as nullable
in onActivityResult
Just replace onActivityResult
signature of SampleActivity with below:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
AsyncTask<CognitoCachingCredentialsProvider, Integer, Void> task = new
AsyncTask<CognitoCachingCredentialsProvider, Integer, Void>() {
@Override
protected Void doInBackground(CognitoCachingCredentialsProvider... params) {
AWSSessionCredentials creds = credentialsProvider.getCredentials();
String id = credentialsProvider.getCachedIdentityId();
credentialsProvider.refresh();
Log.d("wooohoo", String.format("id=%s, token=%s", id, creds.getSessionToken()));
return null;
}
};
task.execute(credentialsProvider);
Check Answer Key 2018