Android : Integrating gallery functionality with camera capture

后端 未结 1 563
攒了一身酷
攒了一身酷 2021-01-23 00:42

I am working on an Android project in which I have the functionality where user can click on the a button and it will open the camera to upload the image. The uploa

1条回答
  •  星月不相逢
    2021-01-23 01:20

    In Constant file write in my case(ActivityConstantUtils.java)

    public static final int GALLERY_INTENT_REQUEST_CODE = 0x000005;
    

    To open Gallery & get path of selected image use following code :

     Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     photoPickerIntent.setType("image/*");
     photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());        
     mActPanelFragment.startActivityForResult(photoPickerIntent, ActivityConstantUtils.GALLERY_INTENT_REQUEST_CODE);
    

    After that you get path in onActivityResult() method

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    (requestCode == ActivityConstantUtils.GALLERY_INTENT_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
    try {
                String imagePath = getFilePath(data);
                // TODO: Here you set data to preview screen
        }catch(Exception e){}
    }
    

    }

    private String getFilePath(Intent data) {
        String imagePath;
        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]);
        imagePath = cursor.getString(columnIndex);
        cursor.close();
    
        return imagePath;
    
    }
    

    0 讨论(0)
提交回复
热议问题