6

someone can tell me what the problem, it is not working, so please help fast i really need:

 imagePick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Contact Image"),1);
        }
    });


  public void onActivityResult(int reqCode, int resCode, Intent data)
{
    if(resCode==RESULT_OK)
    {
        if(reqCode==1) {
            imageURI=data.getData();
            iv.setImageURI(data.getData());

        }
    }
}
Evan Haas
  • 2,524
  • 2
  • 22
  • 34
Nir Ben Yossef
  • 61
  • 1
  • 1
  • 9
  • Please add stacktrace – Sebastian Walla May 02 '15 at 16:25
  • 1
    Please explain what "is not working" means. Also note that using `setImageURI()` is not a good idea. Quoting the docs, "This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup". There are [many image loading libraries for Android](https://android-arsenal.com/tag/46) that can load your image on a background thread. – CommonsWare May 02 '15 at 16:28
  • i am doing an application, and i wanna pick image from gallery when i pick so it is close my app, what is the problem – Nir Ben Yossef May 02 '15 at 16:34
  • "it is close my app" -- if you mean that your app is crashing, use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Also, please bear in mind that there are many Android developer support sites, [in a variety of languages](http://www.andglobe.com). – CommonsWare May 02 '15 at 17:08
  • no man, it is close, not show this message – Nir Ben Yossef May 02 '15 at 17:20

1 Answers1

11

This is working for me.

private final static int SELECT_PHOTO = 12345;

 imagePick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });    

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

        // Here we need to check if the activity that was triggers was the Image Gallery.
        // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
        // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
        if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
            // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
            imageView.setImageBitmap(bitmap);

             // Do something with the bitmap


            // At the end remember to close the cursor or you will end with the RuntimeException!
            cursor.close();
        }
    }
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59