0

I'm having a problem loading an image from the gallery in my Android app. I have some code that picks an image in emulator. When I make a selection, an exception is thrown and caught, meaning the image doesn't load, but I'm not sure how to diagnose what's happening.

Here is my java code file:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    galleryImage = (ImageView) findViewById(R.id.imageView);
    pick_btn = (Button)findViewById(R.id.button);
    pick_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i,RESULT_LOAD_IMG);
        }
    });
}

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

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

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            Log.i("Image Column Index..", "hoiiii");
            assert cursor != null;
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();

            galleryImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }catch (Exception e)
    {
        Toast.makeText(getApplicationContext(),"You haven't picked any image",Toast.LENGTH_LONG).show();
    }
}

}

clearlight
  • 12,255
  • 11
  • 57
  • 75
  • You may be having some permission issue. That's why it is throwing exception. Can you also add exception logs ? – Passiondroid Sep 29 '16 at 06:34
  • 1
    1) Check that is there any images in emulator ? 2) Check in real device instead of emulator. 3) Must be sure that you have given `android.permission.WRITE_EXTERNAL_STORAGE` and `android.permission.READ_EXTERNAL_STORAGE` permissions in manifest file. 4) If your target version is 23 then you must use runtime permission to access that. – Piyush Sep 29 '16 at 06:43
  • Have you defined a permission to access your external storage ? – GrIsHu Sep 29 '16 at 07:02
  • @GrIsHu Ek help kari de ne jo solution janti hoy to http://stackoverflow.com/questions/39741777/image-quality-is-poor-using-custom-camera – Piyush Sep 29 '16 at 07:05

3 Answers3

0

please check user-permission in your manifest file:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
anonymous
  • 401
  • 7
  • 15
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
  • Hii all.. thanks everyone i sorted out this error but adding permission in my Activity file.. i already added permission in manifest.. – abirami bhargavi Sep 29 '16 at 13:21
0

You need to ask runtime permission for WRITE_EXTERNAL_STORAGE before you pick image from gallary.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

declare permissions and request code

private static final String[] REQUEST_PERMISSIONS = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    private static final int REQUEST_PERMISSION_CODE = 007;
//check if permissions are needed

       if (!checkPermission()) {
            requestPermission();
        } else {
//open gallary and pick image      
  }



private boolean checkPermission () {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                return true;
            }
            for (String permission : REQUEST_PERMISSIONS) {
                if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }


            return true;
        }
    private void requestPermission () {
        ActivityCompat.requestPermissions(this, REQUEST_PERMISSIONS, REQUEST_PERMISSION_CODE);
    }

    @Override
    public void onRequestPermissionsResult ( int requestCode, @NonNull String[] permissions,
    @NonNull int[] grantResults){
        if (requestCode == REQUEST_PERMISSION_CODE) {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //open gallary and pick image
            }
        }
    }
Pro Mode
  • 1,453
  • 17
  • 30
0

Here is my edited code:

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

        try
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            {
                if (!Settings.System.canWrite(this) && (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data))
                {
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE}, 2909);

                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

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

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    picturePath = cursor.getString(columnIndex);
                    cursor.close();

                    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                }
            }
        } catch (Exception e)
        {
            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
        }
    }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163