打开Gallery Android后返回活动状态

时间:2016-04-28 01:48:57

标签: java android android-intent android-alertdialog android-gallery

我正在创建一个涉及设置个人资料图片的应用,我的代码基于教程,代码打开了图库,我可以选择图片,但我不明白为什么它不会来我选择它时回到活动。

注意,所有这些都发生在AlertDialog上。

public class ChangePhotoDialog{
    private AlertDialog.Builder builder;
    private View dialoglayout;
    private AlertDialog alertDialog;

    private Firebase mRef;

    public ChangePhotoDialog(Activity a, final SettingsFragment myFrag, final User user){
        LayoutInflater inflater = a.getLayoutInflater();
        dialoglayout = inflater.inflate(R.layout.change_picture, null);

        mRef=new Firebase("https://custom.firebaseio.com/users/"+user.getUserId());

        //Retrieves elements on the change profile picture dialog box
        ImageView oldProfilePicture = (ImageView) dialoglayout.findViewById(R.id.prev_photo);
        final ImageButton newProfileCamera = (ImageButton) dialoglayout.findViewById(R.id.photofromcamera);
        final ImageButton newProfileGallery = (ImageButton) dialoglayout.findViewById(R.id.photofromgallery);

        oldProfilePicture.setImageResource(R.drawable.userdefault);
        //setup dialogue box
        builder = new AlertDialog.Builder(a);
        builder.setView(dialoglayout);

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                System.out.println("Operation Canceled");
            }
        });

        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                System.out.println("Name Changed");
            }
        });

        newProfileCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(myFrag.getActivity().getPackageManager()) != null) {
                    alertDialog.dismiss();
                    myFrag.getActivity().startActivityForResult(takePictureIntent, 1);
                }

            }

        });

        newProfileGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                myFrag.getActivity().startActivityForResult(i, 1);
            }


        });

    }

    public void OnActivityResult (int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

            String imagefile = encodeToBase64(thumbnail, Bitmap.CompressFormat.PNG, 100);

            //Push changes to firebase
            Map<String, Object> changes = new HashMap<String, Object>();
            changes.put("profilePicName", imagefile);
            mRef.updateChildren(changes);

            ImageView oldPicture = (ImageView) dialoglayout.findViewById(R.id.prev_photo);
            oldPicture.setImageBitmap(thumbnail);
        }
    }

    public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
        ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
        image.compress(compressFormat, quality, byteArrayOS);
        return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
    }

    public void show() {
        alertDialog = builder.show();
    }
}

我的理解是,从图库中选择图片后会调用OnActivityResult,但这不会发生。我该如何解决这个问题?

谢谢,如果需要更多信息,请告诉我

0 个答案:

没有答案