Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startActivityForResult is not working in Fragment

I am currently working with with Fragment. I want to pick Image from gallery & display it onTo fragment class. But here startActivityForResult() is not working in Fragment.

My fragment class

Button loadimage=(Button) view.findViewById(R.id.loadButton);
    loadimage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        imgview.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

    }
like image 584
Jigar Shekh Avatar asked Feb 17 '14 10:02

Jigar Shekh


2 Answers

In Activity class:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
}

In Fragment :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    //  Handle activity result here  
}

start activity for result from fragment:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1); 
like image 100
Jaiprakash Soni Avatar answered Oct 11 '22 12:10

Jaiprakash Soni


Override onActivityResult in your parent Fragment Activity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
}
like image 24
Thein Avatar answered Oct 11 '22 14:10

Thein