如何将ImageView设置为Android片段?

时间:2016-04-16 15:41:03

标签: java android android-fragments android-camera android-gallery

我的应用需要个人资料图片拍照或从图库中选择。我在一个片段上,当我点击一个按钮时,我可以选择是拍照还是从图库中选择。该功能有效,我打开相机和图库,但当我返回我的片段时,我看不到图像拍摄或选择进入 ImageView 为什么?

我使用此代码:

AlertDialog选择选项:

final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Add Photo");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (items[item].equals("Take Photo")) {
                            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                            startActivityForResult(intent, REQUEST_CAMERA);
                        } else if (items[item].equals("Choose from Library")) {
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
                        } else if (items[item].equals("Cancel")) {
                            dialog.dismiss();
                        }
                    }
                });
                builder.show();

拍照:

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
 Toast.makeText(getActivity().getApplicationContext(),"OK" , Toast.LENGTH_LONG).show();
etImage.setImageBitmap(thumbnail);

从图库中选择:

Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
etImage.setImageURI(selectedImageUri);           

整个onActivityResult()是

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

        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == REQUEST_CAMERA) {
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                Toast.makeText(getActivity().getApplicationContext(),"OK" , Toast.LENGTH_LONG).show();
                etImage.setImageBitmap(thumbnail);
            } else if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                etImage.setImageURI(selectedImageUri);                
        }

    }         

那么问题是什么? Logcat没有向我显示任何错误

0 个答案:

没有答案