0

我有代码来显示从这样的相机设备捕获的图像

Intent intent    = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    String file_name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
                    File file        = new File(Environment.getExternalStorageDirectory(),
                                        "tmp_avatar_" + file_name + ".jpg");
                    mImageCaptureUri = Uri.fromFile(file);
                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

                    try {   

                        intent.putExtra("return-data", true);
                        intent.putExtra("mImageCaptureUri", mImageCaptureUri); 
                        startActivityForResult(intent, PICK_FROM_CAMERA);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }  

并从路径设置图像位图,当我从纵向视图相机设备捕获图像时它可以工作,但是当我从横向视图相机捕获图像时它会出错,我认为这是因为我检索图像的活动是纵向的。那么你能给我一些建议,以便我可以从肖像或风景相机中捕捉图像吗?谢谢

4

2 回答 2

1

尝试这个

一开始的意图

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 

然后活动结果

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {  
        if(data!=null && resultCode == RESULT_OK)
        {
        bitMapForProfilePic = (Bitmap) data.getExtras().get("data");
        bitMapForProfilePic =Bitmap.createScaledBitmap(bitMapForProfilePic, getWindowManager().getDefaultDisplay().getWidth()/5, getWindowManager().getDefaultDisplay().getHeight()/4, true);
        registration_profilePicID.setImageBitmap(bitMapForProfilePic);           bitMapForProfilePic=null;
        }
      }
    }  

如果你想存储位图

    extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;
   File file = new File(extStorageDirectory, "profilepicture.PNG");
   try {
    outStream = new FileOutputStream(file);
    bitMapForProfilePic.compress(Bitmap.CompressFormat.PNG, 100, outStream);

    outStream.flush();
    outStream.close();


   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

然后设置图像

view.setImageBitmap(BitmapFactory.decodeFile("/sdcard/profilepicture.PNG"));

如果您有任何疑问,请告诉我...

于 2012-11-02T06:25:55.220 回答
0

我在我的一个应用程序中遇到了类似的问题,我已通过以下方式修复它:首先保存您的实例状态:

@Override
protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
state.putString(IMAGE_FILE_PATH, imageFilePath); }

然后恢复它:

@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
ImageFilePath = state.getString(IMAGE_FILE_PATH); }

这对我有用,但是变量 ImageFilePath 对于您的 Activity 应该是全局的,所以也许有更优雅的方法来实现这一点

于 2012-11-02T06:57:48.427 回答