Android相机在纵向和横向模式下保存图像

问题描述:

我正在制作一个android应用程序,并且我想在其中使用相机。我正确地执行了代码,但是我面临的问题是我捕获的图像被保存了两次,一次是水平的,另一次是垂直的。这是一个奇怪的问题,我无法弄清楚。下面是我正在使用的代码。Android相机在纵向和横向模式下保存图像

AlertDialog.Builder builder = new AlertDialog.Builder(
       FaceconActivity.this); 
     builder.setTitle("Share a Photo"); 
     builder.setItems(items, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       if (item == 0) { 
        values = new ContentValues(); 
        values.put(MediaStore.Images.Media.TITLE, "New Picture"); 
        values.put(MediaStore.Images.Media.DESCRIPTION, 
          "From your Camera"); 
        imageUri = getContentResolver().insert(
          MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
          values); 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
        startActivityForResult(intent, PICTURE_RESULT); 
       } else if (item == 1) { 
        try { 
         Intent intent = new Intent(); 
         intent.setType("image/*"); 
         intent.setAction(Intent.ACTION_GET_CONTENT); 
         startActivityForResult(
           Intent.createChooser(intent, "Select Picture"), 
           PICK_IMAGE); 
        } catch (Exception e) { 
         Toast.makeText(getApplicationContext(), e.getMessage(), 
           Toast.LENGTH_LONG).show(); 
         Log.e(e.getClass().getName(), e.getMessage(), e); 
        } 
       } 
      } 
     }); 
     builder.show(); 

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (requestCode) { 
     case PICK_IMAGE: 
      if (resultCode == Activity.RESULT_OK) { 
       Uri selectedImageUri = data.getData(); 
       try { 
        // OI FILE Manager 
        String filemanagerstring = selectedImageUri.getPath(); 
        // MEDIA GALLERY 
        String selectedImagePath = getPath(selectedImageUri); 
        if (selectedImagePath != null) { 
         filePath = selectedImagePath; 
        } else if (filemanagerstring != null) { 
         filePath = filemanagerstring; 
        } else { 
         Toast.makeText(getApplicationContext(), "Unknown path", 
           Toast.LENGTH_LONG).show(); 
         Log.e("Bitmap", "Unknown path"); 
        } 

        if (filePath != null) { 
         decodeFile(filePath); 
        } else { 
         bitmap = null; 
        } 
       } catch (Exception e) { 
        Toast.makeText(getApplicationContext(), "Internal error", 
          Toast.LENGTH_LONG).show(); 
        Log.e(e.getClass().getName(), e.getMessage(), e); 
       } 
      } 
      break; 
     case PICTURE_RESULT: 
      if (requestCode == PICTURE_RESULT) 
       if (resultCode == Activity.RESULT_OK) { 
        try { 
         thumbnail = MediaStore.Images.Media.getBitmap(
           getContentResolver(), imageUri); 
         imageurl = getRealPathFromURI(imageUri); 
         decodeFile(imageurl); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

       } 
     } 
    } 

谁能帮我?
-Thanks在advace

+0

我不认为我面临这个问题更早。它可以是因为android更新。我将我的手机从2.3.4更新到2.3.6。 – Antrromet 2012-04-04 07:58:43

+0

更新:它似乎是一个设备问题,因为相同的代码在其他设备上工作得非常好。 – Antrromet 2012-10-06 17:38:06

项==Ø请使用以下工作代码:::

Calendar cal = Calendar.getInstance(); 
File file = new File(Environment.getExternalStorageDirectory(),(cal.getTimeInMillis()+".jpg")); 
if(!file.exists()){ 
    try { 
     file.createNewFile(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
}else{ 
    file.delete(); 
    try { 
     file.createNewFile(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
imageUri = Uri.fromFile(file); 
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
startActivityForResult(i, PICTURE_RESULT); 

你也可以改变形象的名字,只要你喜欢