Java 打开Gallery Android后返回活动

Java 打开Gallery Android后返回活动,java,android,android-intent,android-alertdialog,android-gallery,Java,Android,Android Intent,Android Alertdialog,Android Gallery,我正在创建一个应用程序,其中包括设置个人资料图片,我的代码基于教程,代码将打开图库,我可以选择图片,但我不明白为什么在选择图片时它不会返回到活动 注意,所有这些都发生在AlertDialog上 public class ChangePhotoDialog{ private AlertDialog.Builder builder; private View dialoglayout; private AlertDialog alertDialog; private

我正在创建一个应用程序,其中包括设置个人资料图片,我的代码基于教程,代码将打开图库,我可以选择图片,但我不明白为什么在选择图片时它不会返回到活动

注意,所有这些都发生在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();
    }
}
公共类更改光电对话{
私有AlertDialog.Builder;
私有视图布局;
私人AlertDialog AlertDialog;
私人消防基地;
公共变更光电对话(活动a,最终设置片段myFrag,最终用户){
LayoutInflater充气机=a.getLayoutInflater();
dialoglayout=充气机。充气(R.layout.change\u图片,空);
mRef=新的火基(“https://custom.firebaseio.com/users/“+user.getUserId());
//检索“更改配置文件图片”对话框中的元素
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);
//设置对话框
builder=新建AlertDialog.builder(a);
builder.setView(对话框布局);
setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
public void onClick(DialogInterface对话框,int-id){
系统输出打印项次(“操作已取消”);
}
});
setPositiveButton(“保存”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
System.out.println(“名称更改”);
}
});
newProfileCamera.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent takePictureIntent=新的意图(MediaStore.ACTION\u IMAGE\u CAPTURE);
if(takePictureContent.resolveActivity(myFrag.getActivity().getPackageManager())!=null){
alertDialog.disclose();
myFrag.getActivity().startActivityForResult(takePictureContent,1);
}
}
});
newProfileGallery.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
alertDialog.disclose();
Intent i=新Intent(Intent.ACTION\u PICK,android.provider.MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
myFrag.getActivity().startActivityForResult(i,1);
}
});
}
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
if(requestCode==1){
位图缩略图=(位图)数据.getExtras().get(“数据”);
字符串imagefile=encodeToBase64(缩略图,Bitmap.CompressFormat.PNG,100);
//将更改推送到firebase
映射更改=新建HashMap();
changes.put(“profilePicName”,imagefile);
mRef.updateChildren(变更);
ImageView oldPicture=(ImageView)dialoglayout.findViewById(R.id.prev_photo);
oldPicture.setImageBitmap(缩略图);
}
}
公共静态字符串encodeToBase64(位图图像、位图.CompressFormat CompressFormat、整数质量){
ByteArrayOutputStream byteArrayOS=新建ByteArrayOutputStream();
压缩(压缩格式、质量、byteArrayOS);
返回Base64.encodeToString(byteArrayOS.toByteArray(),Base64.DEFAULT);
}
公开展览({
alertDialog=builder.show();
}
}
我的理解是,在从图库中选择图片后,将调用OnActivityResult,但事实并非如此。我怎样才能解决这个问题


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

您是否已将
android:noHistory=“true”
添加到清单中的调用活动中?是的,已更改为false,但仍然不起作用。太糟糕了。另一个人通过这一改变设法解决了这个问题。您是否已将
android:noHistory=“true”
添加到清单中的调用活动中?是的,已更改为false,但仍然不起作用。太糟糕了。另一个人通过这一改变设法解决了这个问题。