Daniel
Daniel

Reputation: 1267

My Android app is closing after selecting a photo from Gallery. (There is a video of the bug on the post)

Thanks for your time. I am doing an app where I launch the Gallery from and activity A select an image and comeback to the same activity A.

I am doing it with no problem from a proyect with a single Activity (which is defined as launcher on Manifest) but it is not working properly when I do it from the whole proyect from the an Activity defined on manifest as default.

The Intent I use to open the gallery and select a photo:

public void openGallery(int req_code){

   Intent intent = new Intent();
   intent.setType("image/*");
   intent.setAction(Intent.ACTION_GET_CONTENT);
   startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);
}

The onActivityResult where I get the data (which is never called when I executed from the hole project):

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        Uri selectedImageUri = data.getData();
        if (requestCode == SELECT_FILE1)
        {
            selectedPath1 = getPath(selectedImageUri);
            System.out.println("selectedPath1 : " + selectedPath1);
        }
        if (requestCode == SELECT_FILE2)
        {
            selectedPath2 = getPath(selectedImageUri);
            System.out.println("selectedPath2 : " + selectedPath2);
        }
        tv.setText("Selected File paths : " + selectedPath1 + "," + selectedPath2);
    }
}

This is the logcat dump:

01-05 15:18:14.569: I/ActivityManager(59): Starting activity: Intent {   act=android.intent.action.UPLOADIMAGEDEMO cmp=com.example/.UploadImageDemo }
01-05 15:18:15.779: D/dalvikvm(2496): GC_EXTERNAL_ALLOC freed 2697 objects / 189152 bytes in 92ms
01-05 15:18:16.599: I/ActivityManager(59): Displayed activity com.example/.UploadImageDemo: 1953  ms (total 1953 ms)
01-05 15:18:21.899: D/dalvikvm(279): GC_EXPLICIT freed 871 objects / 134112 bytes in 217ms
01-05 15:18:26.620: I/ActivityManager(59): Starting activity: Intent {  act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity (has extras) }
01-05 15:18:28.250: I/ActivityManager(59): Starting activity: Intent {  act=android.intent.action.GET_CONTENT typ=image/* flg=0x3000000  cmp=com.android.gallery/com.android.camera.ImageGallery }
01-05 15:18:28.670: D/dalvikvm(279): GC_EXTERNAL_ALLOC freed 388 objects / 53352 bytes in 60ms
01-05 15:18:28.850: D/dalvikvm(279): GC_EXTERNAL_ALLOC freed 326 objects / 90664 bytes in 53ms
01-05 15:18:29.200: I/ActivityManager(59): Displayed activity    com.android.gallery/com.android.camera.ImageGallery: 873 ms (total 2345 ms)
01-05 15:18:29.420: D/dalvikvm(279): GC_EXTERNAL_ALLOC freed 725 objects / 106960 bytes in 140ms
01-05 15:18:30.430: W/InputManagerService(59): Starting input on non-focused client         com.android.internal.view.IInputMethodClient$Stub$Proxy@45020da8 (uid=10002 pid=279)
01-05 15:18:32.010: D/dalvikvm(112): GC_FOR_MALLOC freed 9994 objects / 476368 bytes in 149ms

To finish, I recorded the behaviour of the emulator first from the whole project (which quits the app) and second from the separated proyect which works fine.

http://www.youtube.com/watch?v=SntnyKiJQ1Q&feature=youtu.be

Thanks a lot!!!

Upvotes: 3

Views: 1908

Answers (3)

Chirag Prajapati
Chirag Prajapati

Reputation: 347

Remove android:noHistory="true" attribute from your manifest file Like below :

 <activity
        android:name=".YourActivity"
        android:noHistory="true"   <-------------------Remvoe this line
        android:theme="@style/AppTheme"/>

Now Original code will be like this :

 <activity
        android:name=".YourActivity"
        android:theme="@style/AppTheme"/>

Now, It will work perfect. Enjoy.

Upvotes: 0

Manuelvalles
Manuelvalles

Reputation: 380

Try to change in activity manifest :

android:noHistory="false"

Upvotes: 2

Haris ur Rehman
Haris ur Rehman

Reputation: 2663

I have the working code which launches Gallery for picking image with following code:

static final int GALLERY_RESULT = 1;

Intent j = new Intent(Intent.ACTION_PICK, Images.Media.EXTERNAL_CONTENT_URI);
j.setType("image/*");
startActivityForResult(j, GALLERY_RESULT);

and I handle the result as follows:

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case GALLERY_RESULT:
                Uri imgUri = intent.getData();
                imgView.setImageURI(imgUri);
            break;
        }
    }
}

Upvotes: 0

Related Questions