当我从图库中选择图片时,onCreate在onActivityResult之后调用

时间:2013-10-30 16:45:21

标签: android android-intent android-fragments android-activity actionbarsherlock

我的'SherlockFragmentActivity'覆盖了'onActivityResult'。

我尝试从相机和图库中获取图像并裁剪它。 问题是我在onActivityResult调用后返回我的活动而不是片段。

...
            FragmentTransaction t = fragmentManager.beginTransaction();
            LogInFragment logFrag = new LogInFragment();
            t.replace(R.id.fragment_container, logFrag);
            t.commit();
...
     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
        }

活动布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/fragment_container"
                android:background="@color/textWhite"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

</RelativeLayout>

我也有'SherlockFragment',我选择了图片:

startImagePickerDialog(this);


public void startImagePickerDialog() {
        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(getSherlockActivity());
        myAlertDialog.setTitle("Upload Pictures Option");
        myAlertDialog.setMessage("How do you want to set your picture?");

        myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Intent intent = new Intent();
                // call android default gallery
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                // ******** code for crop image
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                intent.putExtra("noFaceDetection", true);
                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(Intent.createChooser(intent,
                            "Complete action using"), Const.GALLERY_PICTURE);
                } catch (ActivityNotFoundException e) {
                    Log.e(LOG_TAG, "ActivityNotFoundException");
                }
            }
        });
        myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // call android default camera
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                intent.putExtra("noFaceDetection", true);
                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, Const.CAMERA_REQUEST);
                } catch (ActivityNotFoundException e) {
                    Log.e(LOG_TAG, "ActivityNotFoundException");
                }
            }
        });
        myAlertDialog.show();
    }

'SherlockFragment'中的'onActivityResult':

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

        if (resultCode != getSherlockActivity().RESULT_OK) {
            Log.e(LOG_TAG, "resultCode != RESULT_OK");
            return;
        }

        if (requestCode == Const.CAMERA_REQUEST) {
            Log.d(LOG_TAG, "requestCode == CAMERA_REQUEST");
            Bundle extras = data.getExtras();
            if (extras != null) {
                Log.d(LOG_TAG, "extras != null");
                Bitmap photo = extras.getParcelable("data");
                icon.setImageBitmap(photo);
            }
        }

        if (requestCode == Const.GALLERY_PICTURE) {
            Log.d(LOG_TAG, "requestCode == GALLERY_PICTURE");
            Bundle extras2 = data.getExtras();
            if (extras2 != null) {
                Log.d(LOG_TAG, "extras != null");
                Bitmap photo = extras2.getParcelable("data");
                icon.setImageBitmap(photo);
            }
        }
    }

更新 当我调用相机活动时,我的主要活动调用'onSaveInstanceState',然后调用'onRestoreInstanceState'。这是一个原因吗?

3 个答案:

答案 0 :(得分:11)

  1. 检查“设置” - &gt; “开发者选项” - &gt; “不要保持活动”的旗帜。
  2. 这是android的本质,如果你的设备需要内存它会破坏不可见的活动。因此,您必须考虑可以随时重新创建您的活动。 BTW“不要保持活动”选项可以在您的设备需要内存并破坏您的后台活动时模拟您的应用程序。

答案 1 :(得分:0)

尝试这样

更改

Bitmap photo = extras.getParcelable("data");

Bitmap photo=(Bitmap) intent.getExtras().get("data");

<强>编辑: -

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

            if (resultCode != getSherlockActivity().RESULT_OK) {
                Log.e(LOG_TAG, "resultCode != RESULT_OK");
                return;
            }

            if (requestCode == Const.CAMERA_REQUEST) {
                Log.d(LOG_TAG, "requestCode == CAMERA_REQUEST");
                Bitmap photo=(Bitmap) intent.getExtras().get("data");// Changed Here
                icon.setImageBitmap(photo);

            }

            if (requestCode == Const.GALLERY_PICTURE) {
                Log.d(LOG_TAG, "requestCode == GALLERY_PICTURE");
               Uri imageUri= intent.getData();// Changed Here, or first decode the image to Avoid OutOfMemory Error
               icon.setImageURI(imageUri);
            }
        }

答案 2 :(得分:0)

请替换

frag.startActivityForResult(...); 

在你的片段中

startActivityForResult(...)