onCreate and onRestoreInstanceState(Bundle) not called after startActivityForResult - android

I'm trying to load an image from gallery.
This is the onCreate() :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_picture);
mThis = this;
mImageCache = new TakingPictureActivityCacheMngr(this);
initUi();
if (savedInstanceState == null) {
openGallery();
}
}
Here is the onSaveInstanceState():
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", mFileUri);
if (com.isee.spot.toolkit.Config.IS_DEBUG) {
Log.d(TAG, "Instance was saved.");
}
}
On restore : (never called)
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file Uri
mFileUri = savedInstanceState.getParcelable("file_uri");
}
The onCreate() is never called after opening the gallery, onDestroy() is called right when the gallery is opened and so it the onSaveInstanceState().
Here is the activity declaration in the manifest :
<activity
android:name=".GalleryPictureActivity"
android:configChanges="orientation|keyboard|keyboardHidden"
android:screenOrientation="portrait" >
</activity>
I overloaded the onActivityResult() :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if the result is loading image from gallery
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
...

Here is the answer,
The activity itself was correct.
The problem is with the activity which start it, I used : galleryPic.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Apparently this cause the onActivityResult(int, int, Intent) not be called.
Leaving this just for the chance of someone getting same behavior.
Thank you for trying to help, I guess you had no chance.

I may not be understanding the context of this code properly, but it looks like this is a custom image picking class ? You may be aware - but perhaps not - that there are built in SDK methods for this... see below:
private void pickImage() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_PICK_IMAGE);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
Log.v(TAG, "User Picked An Image");
handleImage(data.getData());
}
else if (resultCode == Activity.RESULT_CANCELED) {
Log.v(TAG, "User Cancelled Pick Image");
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
private void handleImage(final Uri selectedImage) {
String filePath = selectedImage.getEncodedPath();
Log.v(TAG, " -- picked image is " + filePath);
MyImageView.setImage(selectedImage);
}

Related

Image taken from gallery doesn't appear

I use Picasso to get an image from the gallery and set it to an ImageView, but it does not do it. Couldn't find a problem. What is the reason? And the interesting thing is that there was no error. I tested the program through my own device.
public class MainActivity extends AppCompatActivity {
String imageUri ;
ImageView img ;
private static final int GALLERY_REQUEST = 9391;
Button b ;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
imageUri = data.getData().toString() ;
loadImage() ;
}
else
{
super.onActivityResult(requestCode, resultCode, data);
}
}
private void loadImage() {
Picasso.with(this).load(imageUri).fit().centerInside().into(img);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.image);
b = (Button)findViewById(R.id.button) ; // it is button used to open //a gallery
}
//thins function called when button pressed
public void openGallery(View view) {
Intent i = new Intent(ACTION_PICK,EXTERNAL_CONTENT_URI) ;
startActivityForResult(i,GALLERY_REQUEST);
}
}
You called super.onActivityResult(requestCode, resultCode, data); which is wrong .
Do this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
Uri selectedImageURI = data.getData();
Picasso.with(this).load(selectedImageURI).fit().centerInside().into(img);
}
else
{
// handle this case
}
}
Problem Solved.
I forgot to add permission used to read external storage.

Android Camera passing bitmap from Camera Intent to MainActivity

I am trying to take a picture and set it in my MainActivity to an ImageView.
I want to have my camera activity as a seperate class so i tried the following:
MainActivity:
public void onClick(View v) {
switch (v.getId()) {
case R.id.action_button:
Intent camera = new Intent(MainActivity.this,Camera.class);
startActivityForResult(camera ,CAMERA_IDENTIFITER);
break;
default:
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
Log.d("onActivityResult","Sucessfully callbacked!"); //This one wont be executed!
switch(requestCode) {
case (CAMERA_IDENTIFITER) : {
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
mImageView.setImageBitmap(bitmap);
}
break;
}
}
}
And this is my camera Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pm = getPackageManager();
}
#Override
protected void onStart() {
super.onStart();
if(!isFromActivityResult){
dispatchTakePictureIntent();
}
}
public void dispatchTakePictureIntent() {
if (takePictureIntent.resolveActivity(pm) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch {...}
if (photoFile != null) {
pictureTaken = true;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
try {
bitmap = ImageFileHandler.handleSamplingAndRotationBitmap(this, Uri.fromFile(new File(mCurrentPhotoPath)));
//mImageView.setImageBitmap(bitmap);
resultIntent = new Intent();
resultIntent.putExtra(MediaStore.EXTRA_OUTPUT,
bitmap);
setResult(Activity.RESULT_OK, resultIntent);
Log.d("onActivityResult","finish() executed!"); //This one gets executed!
finish();
} catch {...}
}
}
The camera intent is starting properly but when i take a picture i dont get back to the onActivityResult in my MainActivity , i'm stuck in the Camera intent.
Your onStart() gets called after onActivityResult() is being executed. Hence starting camera intent again and again. Put Logs and test your self.
To solve the issue
Keep a boolean variable in your activity isFronActivityResult. Keep default value as false and do isFromActivityResult = true; inside onActivityResult(). In onStart do the following:
#Override
protected void onStart() {
super.onStart();
if(!isFromActivityResult)
dispatchTakePictureIntent();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
try {
bitmap = ImageFileHandler.handleSamplingAndRotationBitmap(this, Uri.fromFile(new File(mCurrentPhotoPath)));
//mImageView.setImageBitmap(bitmap);
resultIntent = new Intent();
resultIntent.putExtra(MediaStore.EXTRA_OUTPUT,
bitmap);
setResult(Activity.RESULT_OK, resultIntent);
isFromActivityResult = true;// add this line
finish();
} catch {...}
}
}

Wait for activity to finish in non-activity class

I have a method in my application which starts a new Activity which starts the Camera Application. The Camera Application returns a result (picture taken or not taken), and I store this result in the Intent-Bundle.
In the original method I want to read this bundle, which does not work because the activity is not finished yet.
How can I wait for the activity to finish before going on in my method?
Method
#Override
public boolean startChallenge(Context context) {
Intent cam = new Intent(context, CameraIntent.class);
cam.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(cam);
Boolean done = cam.getExtras().get("done"); // << this fails obviously
}
Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent = getIntent();
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
intent.putExtra("done", true);
} else if (resultCode == RESULT_CANCELED) {
intent.putExtra("done", false);
}
}
finish();
}
I would like to keep the structure of the code because the startChallenge() Method is inherited from a superclass (Challenge) and startChallenge is different from type to type (e.g. there is a challenge where you have to take a picture, another challenge where you have to answer something, and so on). The method gets called in another activity, depending on the type of challenge.
startChallenge should have a reference to the Activity that expects the result back to be able to call startActivityForResult
Method
#Override
public boolean startChallenge(Activity activityB) {// Activity B in which you are expecting this result back.
Intent cam = new Intent(activityB, ActivityA.class);
activityB.startActivityForResult(cam, REQUEST_CODE_OPEN_CAMERA);
}
Activity A
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent = getIntent();
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
intent.putExtra("done", true);
} else if (resultCode == RESULT_CANCELED) {
intent.putExtra("done", false);
}
}
setResult(RESULT_OK, intent);
finish();
}
Another approach would be to use LocalBroadcastManager or Otto
Activity A
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
BusProvider.getInstance().post(new ImageCapturedEvent(true));
} else if (resultCode == RESULT_CANCELED) {
BusProvider.getInstance().post(new ImageCapturedEvent(false));
}
}
finish();
}

Camera onActivityResult on fragment

I'm working on some code someone else wrote and I'm having trouble handling the result of a camera intent.
Basically i have a DashBoardActivity which contains a tab with a fragment called "MyProfileContainer", which contains a "SettingsFragment" fragment which contains a "EditProfileFragment"fragment.
In the EditProfileFragment the user can take a picture for his profile. It works but it calls the onActionResult on the Dashboard Activity.
I read some guide on how to redirect it to the EditProfileFragment but I haven't been able to do it.
I'm losing literally days on this one and I can't figure it out.
This is onActivityResult on the Dashboard Activity
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
And this is EditProfileFragment
private Uri imageUri = null;
public void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
getParentFragment().startActivityForResult(intent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getActivity().getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
profilePhoto.setImageBitmap(bitmap);
Toast.makeText(getActivity(), selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getActivity(), "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
I don't know if I have to override the method on every class between this two or if I'm doing something else wrong, but I'm sure the EditProfileFragment onActivityResult is never called.
I found out the problem, and it was actually a bug of Android.
The fragment that was to recieve the result was a fragment nested inside other fragment and the method call was not properly propagated that deeply.
I had to override the method on the containing fragment manually and it worked.
The easy trick to call OnActivityResult in nested fragment.
1) Add this code in your captureImage method it will start a new activity.
Intent intent = new Intent(getContext(), CameraPreviewActivity.class);
startActivityForResult(intent, 111);
2) Now CameraPreviewActivity activity will open and it will start camera activity and returns the result to fragment.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, FragmentAccPhoto.REQUEST_IMAGE_CAPTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setResult(RESULT_OK, data);
finish();
}

Null pointer exception when calling startActivityForResult()

I'm having a problem (Null Pointer Exception) when calling startActivityForResult() from class inside another class, here is the code :
public class OCRActivity extends Activity {
public OCRActivity(String operator)
{
this.operator = operator;
}
public void startCameraActivity() {
final Intent capture_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(capture_intent, 1); // I get the Null pointer Exception here
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == 1){
//get the Uri for the captured image
picUri = data.getData();
//carry out the crop operation
performCrop();
}
}
and here is where I call startCameraActivity()
public class WayToFillActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.way_to_fill);
CAMERA_BTN = (Button) findViewById(R.id.camera_btn);
CAMERA_BTN.setOnClickListener(this);
Intent operator_intent = getIntent();
OPERATOR = operator_intent.getStringExtra("operator");
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.camera_btn)
{
OCRActivity ocr = new OCRActivity(OPERATOR);
ocr.startCameraActivity();
}
}
So please tell me if I'm doing something wrong !!
The Logcat
01-13 16:22:26.583: E/AndroidRuntime(32425): java.lang.NullPointerException
01-13 16:22:26.583: E/AndroidRuntime(32425): at android.app.Activity.startActivityForResult(Activity.java:3190)
01-13 16:22:26.583: E/AndroidRuntime(32425): at com.almannaa.EasyRecharge.OCRActivity.startCameraActivity(OCRActivity.java:176)
You are treating OCRActivity as an ordinary Java class, and not like another Activity. Due to this, when you call startActivityForResult() you get a NPE as the Activity's onCreate() has not been called, which means that its current instance is not valid for calling startActivityForResult()
Instead, try using:
public class WayToFillActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.way_to_fill);
CAMERA_BTN = (Button) findViewById(R.id.camera_btn);
CAMERA_BTN.setOnClickListener(this);
Intent operator_intent = getIntent();
OPERATOR = operator_intent.getStringExtra("operator");
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.camera_btn)
{
OCRActivity ocr = new OCRActivity(OPERATOR);
ocr.startCameraActivity();
}
}
public void startCameraActivity() {
final Intent capture_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(capture_intent, 1); // I get the Null pointer Exception here
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == 1){
//get the Uri for the captured image
picUri = data.getData();
//carry out the crop operation
performCrop();
}
}
}
You should probably move functions like performCrop() (and any other functions that don't really need to be in the activity itself, and can work by receiving data as parameters and returning it) into a separate class (call it Utility or whatever).
Maybe you should check if the data is not null.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && data != null) {
if(resultCode == RESULT_OK){
//get the Uri for the captured image
picUri = data.getData();
//carry out the crop operation
performCrop();
} }

Categories

Resources