2015-04-19 38 views
0

我正在做/試圖做的是。 1.拍照 2.保存它 3.加載/顯示它變成一個位圖Android:我如何顯示這個圖像到位圖?

打開內置在相機應用程序:

public void openCamera() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "image.jpg"); 
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 
    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
} 

onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     //Check that request code matches ours: 
     if (requestCode == REQUEST_IMAGE_CAPTURE){ 
      //Get our saved file into a bitmap object: 
      File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); 
      Bitmap image = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700); 
     } 
    } 

decodeSamoleBitmapFromFile:

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

我希望它將它加載到位圖中。如果有人能指出我的方向,這將是非常有益的!

+3

什麼問題? – Emmanuel

+0

我似乎無法將圖像顯示到位圖 – smither123

+0

您解碼了位圖。你在哪裏展示它? – Raiv

回答

0

爲了顯示位圖,您必須使用ImageView。一旦你有位圖和對你的ImageView的引用,請致電ImageView.setImageBitmap(Bitmap)來顯示你的位圖。