Choose an image from camera or gallery

大憨熊 提交于 2019-12-10 00:05:21

问题


How can I offer the user the option of choosing an image from the camera or gallery and retrieve the Resource id associated?


回答1:


Try this as an intent

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

Here is how you retreive the returned image.

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

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    }
}
}


来源:https://stackoverflow.com/questions/8498259/choose-an-image-from-camera-or-gallery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!