1

I have an activity in my library project. From that activity, I am trying to get an image from library. 1 out of thousand times(metaphor), the code below works fine. But mostly, after selecting an image , application gets stuck at black screen.

 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 intent.setType("image/* video/*");
 startActivityForResult(intent, REQUEST_GALLERY);

LOG

This is the success scenario :

06-22 11:22:16.559 19038-19038/com.xyz D/SELECT_IMAGE: onCreate
06-22 11:22:16.869 19038-19038/com.xyz D/SELECT_IMAGE: onResume
06-22 11:22:24.949 19038-19038/com.xyz D/SELECT_IMAGE: Before startActivityForResult
06-22 11:22:24.959 19038-19038/com.xyz D/SELECT_IMAGE: After startActivityForResult
06-22 11:22:32.359 19038-19038/com.xyz D/SELECT_IMAGE: RESULT onActivityResult
06-22 11:22:32.429 19038-19038/com.xyz D/SELECT_IMAGE: onResume

After this, I tried again, then just these two logs, and then stuck

06-22 11:23:02.919 19038-19038/com.xyz D/SELECT_IMAGE: Before startActivityForResult
06-22 11:23:02.929 19038-19038/com.xyz D/SELECT_IMAGE: After startActivityForResult

EDITED : log I got with adb shell dumpsys activity top

java.lang.IndexOutOfBoundsException: Invalid index 6, size is 3
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    at java.util.ArrayList.get(ArrayList.java:308)
    at com.android.server.am.ActivityStack.getDumpActivitiesLocked(ActivityStack.java:6544)
    at com.android.server.am.ActivityStackSupervisor.getDumpActivitiesLocked(ActivityStackSupervisor.java:5734)
    at com.android.server.am.ActivityManagerService.dumpActivity(ActivityManagerService.java:17777)
    at com.android.server.am.ActivityManagerService.dump(ActivityManagerService.java:16936)
    at android.os.Binder.dump(Binder.java:334)
    at android.os.Binder.onTransact(Binder.java:292)
    at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:2568)
    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3242)
    at android.os.Binder.execTransact(Binder.java:461)
BST Kaal
  • 2,993
  • 6
  • 34
  • 52

1 Answers1

-1

Use this with on activityResult like this

public class MainActivity extends AppCompatActivity {
final static int REQUEST_GALLERY=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/* video/*");
    startActivityForResult(intent, REQUEST_GALLERY);
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
                                Intent intent) {
    Toast.makeText(getApplicationContext(),"Recieved",Toast.LENGTH_LONG).show();
     if (requestCode==REQUEST_GALLERY)
    {
        if(responseCode==RESULT_OK)
        {
            Uri selectedImage = intent.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.img_view);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
        }
        }
    }
Sushant
  • 254
  • 1
  • 5
  • 15
  • 1
    Thnx, but the issue is, the app gets stuck even before coming back to `onActivityResult` – BST Kaal Jun 22 '16 at 07:35
  • Please check ur request code and the location where u have used the code for startActivityforResult . Its working perfectly here – Sushant Jun 22 '16 at 07:46