0

当我从相机回来时,我的应用程序崩溃了。应用程序在 bellow 19 Android 版本上完美运行,但不适用于 OS 版本 19。我正在使用 google nexus 7 运行此应用程序。

我正在向您发送我的代码:

cameraButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {


             Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

              startActivityForResult(cameraIntent, IMAGE_CAPTURE);
        }
    });

我的 onActivityResult( 在下面给出):

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
    if (requestCode == IMAGE_CAPTURE) {

        Uri fileUri = null;

        if(imageReturnedIntent == null || resultCode != RESULT_OK){
            Log.d("ResultNotOk", "resultCode"+resultCode);
            File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString());
            for(File f : imageStorageDir.listFiles()){
                if(Uri.fromFile(f).equals(this.fileUri)){
                    fileUri = this.fileUri;

                    Log.d("fileUriNew", "fileUriNew:-" +fileUri);
                    break;
                }
            }

        }else if (resultCode == RESULT_OK) {
            Log.d("ResultOk!!", "resultCode"+resultCode);
            fileUri = imageReturnedIntent.getData();
            }
            //fileUri = imageReturnedIntent.getData();
            Log.d("Uri", "FileUri"+fileUri);

            Intent cameraIntent = new Intent(this, EditImage.class);
            cameraIntent.setType("/*image");
            cameraIntent.setData(imageReturnedIntent.getData());
            cameraIntent.putExtra("EditMode", true);
            cameraIntent.putExtra("Uri", fileUri);
            Log.d("imageReturnedIntent", "imageReturnedIntent"+ imageReturnedIntent);


            //if edit mode is 1, intent is from camera

            cameraIntent.putExtra("EditMode", false);
            startActivity(cameraIntent);
        }
4

1 回答 1

2
   cameraButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

String fileName = "Camera_Example.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            values.put(MediaStore.Images.Media.DESCRIPTION,
                    "Image capture by camera");

            imageUri = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
             Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

              startActivityForResult(cameraIntent, IMAGE_CAPTURE);
        }
    });


 protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            if (imageUri != null) {
                outState.putString("cameraImageUri", imageUri.toString());
            }
        }

        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            if (savedInstanceState.containsKey("cameraImageUri")) {
                imageUri = Uri
                        .parse(savedInstanceState.getString("cameraImageUri"));
            }
        }
于 2014-02-15T07:49:05.083 回答