【问题标题】:Android : force close where get picture image from camera landscapeAndroid:强制关闭从相机风景中获取图片的位置
【发布时间】:2012-11-02 06:06:43
【问题描述】:

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

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();
                    }  

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

【问题讨论】:

标签: android


【解决方案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"));

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

【讨论】:

    【解决方案2】:

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

    @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 应该是全局的,所以也许有更优雅的方法来实现这一点

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 2015-08-25
      相关资源
      最近更新 更多