0

I am integrating a code for how to capture image and how to get image from gallery.Here is my source code. its working fine for individual but it didn't show the imageview when upload image from gallery. please help me

private static int RESULT_LOAD_IMAGE = 1;
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView,imageView1;
private static final int SELECT_PICTURE = 1;
String  selectedPath;

private String selectedImagePath;
Uri selectedImageUri;
//ADDED
private String filemanagerstring;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });
}
ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        //Log.d(TAG, "onShutter'd");
    }
};


protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    } 



    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}


protected void onActivityResult1(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 = 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.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }


}

}

1 Answers1

0

Declare Global Variables just after your class declaration at the top:

 private static int RESULT_LOAD_IMAGE = 1;
 private static final int PICK_FROM_GALLERY = 2;
 Bitmap thumbnail = null; 

Call the intent like this: (Yourfunction)

  public void YourFunction(){
       Intent in = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                      startActivityForResult(in, RESULT_LOAD_IMAGE);

   }

Handle the result like this: Declare this outside of your onCreate anywhere in the class.

 protected 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 = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            thumbnail = (BitmapFactory.decodeFile(picturePath));

Thumbnail is your image, go play with it!

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • Still not working directly when click on gallery button, gallery is not working first have to click on camera button after that only gallery button is working.please help me. – user3145981 Dec 30 '13 at 10:17
  • What are you testing on? – Skynet Dec 30 '13 at 10:17
  • Check this: http://stackoverflow.com/questions/15248265/camera-intent-not-working-with-samsung-galaxy-s3/15287164#15287164\ – Skynet Dec 30 '13 at 10:18
  • Whats wrong in my code? i didnt understand it is working individually when the code is placed in individual classes. – user3145981 Dec 30 '13 at 10:28
  • Result will be delivered to only that class where you define the intent and also where you define the onActivity result. It depends on what specific problem you have. – Skynet Dec 30 '13 at 10:30