2013-04-03 143 views
4

以下代碼可以很好地獲取從相機拍攝的照片的縮略圖。我想獲得完整的圖像,而不僅僅是縮略圖。我有一個三星的Galaxy Nexus從相機獲取圖像到ImageView Android

private static final int CAMERA_PIC_REQUEST = 1111; 

private ImageView mImage; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my_camera); 
     mImage = (ImageView) findViewById(R.id.imageView1); 
     Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, CAMERA_PIC_REQUEST); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAMERA_PIC_REQUEST) { 
      Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
      mImage.setImageBitmap(thumbnail); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
      File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); 
      try { 
       file.createNewFile(); 
       FileOutputStream fo = new FileOutputStream(file); 
       fo.write(bytes.toByteArray()); 
       fo.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
+0

更改ImageView的'wrap_content'中的寬度和高度 – 2013-04-03 23:10:23

回答

8

得到你需要先將其保存到文件,然後提取它是從它的位圖一個全尺寸的圖片,這樣做:在onActivityResult

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 
startActivityForResult(intent, CAPTURE_NEW_PICTURE); 

然後:

File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); 
mImage = (ImageView) findViewById(R.id.imageView1); 
mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250)); 

decodeSampledBitmapFromFile

public static Bitmap decodeSampledBitmapFromFile(String path, 
     int reqWidth, int reqHeight) { // BEST QUALITY MATCH 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     options.inPreferredConfig = Bitmap.Config.RGB_565; 
     int inSampleSize = 1; 

     if (height > reqHeight) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } 

     int expectedWidth = width/inSampleSize; 

     if (expectedWidth > reqWidth) { 
      //if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize.. 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 


    options.inSampleSize = inSampleSize; 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 

    return BitmapFactory.decodeFile(path, options); 
    } 

您可以使用數字(在這種情況下爲500和250)來更改ImageView的位圖質量。

+0

非常感謝,但我將圖像保存在某個不明顯的地方,是否可以將它保存到圖像庫中? – 2013-04-04 15:42:27

+0

圖片庫只是您的手機中包含你的照片的另一個文件夾。我認爲這不重要。 「不明顯」是什麼意思? – 2013-04-04 15:44:28

+0

通過不明顯我的意思是一般用戶不容易找到圖片,因爲它不存儲在圖片庫文件夾 – 2013-04-04 18:21:53

相關問題