Code example for Fragment

Methods: getActivity, onActivityResult

0
        return view;
    } 
 
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_IMAGE) {
                Uri selectedImageUri = data.getData();
 
                //OI FILE Manager 
                String fileManagerString = selectedImageUri.getPath();
 
                //MEDIA GALLERY 
                String selectedImagePath = getPath(selectedImageUri);
 
                String path;
                if (selectedImagePath != null) {
                    path = selectedImagePath;
                } else { 
                    path = fileManagerString;
                } 
                Util.debug("Imagepath: '" + path + "'.");
                Bitmap bitmap = Util.decodeBitmap(path, 800);
                image = Util.bitmapToBytes(bitmap);
                attachImage.setImageBitmap(bitmap);
                attachButton.setVisibility(View.GONE);
                attachImage.setVisibility(View.VISIBLE);
            } 
        } 
    } 
 
    private String getPath(Uri uri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL 
            // THIS CAN HAPPEN IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA 
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();