相机Intent后Android崩溃了

Jad*_*eld 22 camera android android-intent

我发布了一个应用程序,其中一个基本功能是允许用户拍照,然后将该照片保存在其外部存储的特定文件夹中.

一切似乎都运行正常,但我现在有两个报告声称在拍照后,点击"完成"退出相机(并返回活动),应用程序被强制关闭,带来用户回到主屏幕.

这发生在Samsung Nexus S和Galaxy Tab上.下面我发布了我的代码,以显示我设置我的意图以及如何在onActivityResult()中处理保存和显示照片.任何关于在点击"完成"退出相机应用程序后可能导致崩溃的指导,将不胜感激!

同样,这似乎在大多数设备上都能正常工作,但我想知道它们是否是一种更有效,更通用的方法.谢谢

我如何解雇相机意图

   case ACTION_BAR_CAMERA:

        // numbered image name
        fileName = "image_" + String.valueOf(numImages) + ".jpg";


        output = new File(direct + File.separator + fileName); // create
                                                                    // output
        while (output.exists()) { // while the file exists
            numImages++; // increment number of images
            fileName = "image_" + String.valueOf(numImages) + ".jpg";
            output = new File(outputFolder, fileName);


        }
        camera = new   Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        uriSavedImage = Uri.fromFile(output); // get Uri of the output
        camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); //pass in Uri to camera intent
        startActivityForResult(camera, 1);


        break;
    default:
        return super.onHandleActionBarItemClick(item, position);
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我是如何设置onActivityResult()的

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) { // If data was passed successfully

        Bundle extras = data.getExtras();

        //Bundle extras = data.getBundleExtra(MediaStore.EXTRA_OUTPUT);

        /*ad = new AlertDialog.Builder(this).create();
        ad.setIcon(android.R.drawable.ic_menu_camera);
        ad.setTitle("Save Image");
        ad.setMessage("Save This Image To Album?");
        ad.setButton("Ok", this);

        ad.show();*/



        bmp = (Bitmap) extras.get("data"); // Set the bitmap to the bundle
                                            // of data that was just
                                            // received
        image.setImageBitmap(bmp); // Set imageview to image that was
                                    // captured
        image.setScaleType(ScaleType.FIT_XY);


    }

}
Run Code Online (Sandbox Code Playgroud)

ρяσ*_*я K 37

首先让我们说清楚 - 我们有两个选项可以从Camera中获取onActivityResult中的图像数据:

1. 通过传递要保存的图像的确切位置Uri来启动相机.

2. Just Start Camera不传递任何Loaction Uri.


1.在第一种情况下:

通过将图像Uri传递到要保存的位置来启动相机:

String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";  
File imageFile = new File(imageFilePath); 
Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri

Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
startActivityForResult(it, CAMERA_RESULT);
Run Code Online (Sandbox Code Playgroud)

在onActivityResult中接收图像为:

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

    if (RESULT_OK == resultCode) { 
        iv = (ImageView) findViewById(R.id.ReturnedImageView); 

        // Decode it for real 
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = false; 

        //imageFilePath image path which you pass with intent 
        Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions); 

        // Display it 
        iv.setImageBitmap(bmp); 
    }    
} 
Run Code Online (Sandbox Code Playgroud)

2.在第二种情况下:

如果要在Intent(数据)中接收图像,请启动相机而不传递图像Uri:

Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(it, CAMERA_RESULT); 
Run Code Online (Sandbox Code Playgroud)

在onActivityResult中将图像恢复为:

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

    if (RESULT_OK == resultCode) { 
        // Get Extra from the intent 
        Bundle extras = data.getExtras(); 
        // Get the returned image from extra 
        Bitmap bmp = (Bitmap) extras.get("data"); 

        iv = (ImageView) findViewById(R.id.ReturnedImageView); 
        iv.setImageBitmap(bmp); 
    } 
} 
Run Code Online (Sandbox Code Playgroud)


*****快乐的编码!!!!*****

  • 当我使用第二个版本时...我得到的只是缩略图,但我需要真实的图片.如果我使用第二个选项,我会得到真实的图片,但它在三星设备中崩溃了...... (3认同)
  • 这是正确的答案,但任何人都可以告诉我如何知道设备支持位置uri与否? (2认同)

san*_*san -1

我遇到了同样的问题。显然,修复方法是将 uriSavedImage 设为静态。不确定这是否是最好的方法。但这对我有用。