如何在Fragment中从库中选择图像时裁剪选项集

时间:2015-10-20 12:40:44

标签: java android

在我的应用程序中,我有用于从图库中获取图像并进行裁剪的书面代码,并设置为图像视图但图像未设置。如果我没有使用裁剪选项编码意味着它的工作正常。

我不知道如何在Fragment中使用裁剪选项。

这是我的代码:

ProfileFragment:



public class ProfileFragment extends Fragment {
   

    private ImageView imageView;
    private static final int SELECT_PHOTO = 1;
   
    private Uri mSelectedImageUri = null;


    Button browseProfilePic;


    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true);//For option menu

        View view = inflater.inflate(R.layout.fragment_layout_profilepic, container,
                false);

        imageView = (ImageView)view.findViewById(R.id.profile_image);
       
        browseProfilePic = (Button) view.findViewById(R.id.btn_pick);

        browseProfilePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                // Show only images, no videos or anything else
                intent.setType("image/*");
          //Crop option
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX",0);
                intent.putExtra("aspectY",0);
                intent.putExtra("outputX",200);
                intent.putExtra("outputY",150);
                //intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.putExtra("return-data",true);
                // Always show the chooser (if there are multiple options available)
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO);

            }
        });

        return view;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.d("Request Code:", String.valueOf(requestCode));
        Log.d("Result Code:", String.valueOf(resultCode));
        Log.d("Data:",String.valueOf(data));

        if (requestCode == SELECT_PHOTO && data != null) {
            mSelectedImageUri = data.getData();
            Log.d("Uri:", String.valueOf(mSelectedImageUri));
            //User had pick an image.
            Cursor cursor = getActivity().getContentResolver().query(mSelectedImageUri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
            cursor.moveToFirst();
            //Link to the image
            final String imageFilePath = cursor.getString(0);
            cursor.close();
            File file = new File(imageFilePath);
            Log.d("File:", String.valueOf(file));
            if (file != null) {
               Bitmap bMap = BitmapFactory.decodeFile(String.valueOf(file));
               imageView.setImageBitmap(bMap);

            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}




请任何人帮忙修复!

提前致谢..

0 个答案:

没有答案