Android從Camera中獲取圖片的兩種方法

jopen 11年前發布 | 48K 次閱讀 Android Android開發 移動開發

方法一:

此方法會由Camera直接產生照片回傳給應用程序,但是返回的是壓縮圖片,顯示不清晰

 try {
         Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         startActivityForResult(cameraIntent, CAMERA_WITH_DATA);
     } catch (ActivityNotFoundException e) {
         e.printStackTrace();
     }

 

Bundle bundle = data.getExtras();
bmp_selectedPhoto = (Bitmap) bundle.get("data");
if (bmp_selectedPhoto != null)
    bmp_selectedPhoto.recycle();
bmp_selectedPhoto = (Bitmap) data.getExtras().get("data");
// int scale = ImageThumbnail.reckonThumbnail(bitMap.getWidth(),
// bitMap.getHeight(), 500, 600);
// bitMap = ImageThumbnail.PicZoom(bitMap,
// (int) (bitMap.getWidth() / scale),
// (int) (bitMap.getHeight() / scale));
// bitMap = ThumbnailUtils.extractThumbnail(bitMap, 200, 200);
if(bmp_selectedPhoto != null){
    home_view.setBackground(new BitmapDrawable(getResources(),
    bmp_selectedPhoto));
}

方法二:

此方法所拍即所得,但是會在Sd卡上產生臨時文件

Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        File appDir = new File(Environment.getExternalStorageDirectory()
                + "/KengDieA");

        if (!appDir.exists()) {
            appDir.mkdir();
        }

        mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
                + "/KengDieA/", "kengDiePic"
                + String.valueOf(System.currentTimeMillis()) + ".jpg"));
        cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

        try {
            cameraIntent.putExtra("return-data", true);
            startActivityForResult(cameraIntent, CAMERA_WITH_DATA);
        } catch (Exception e) {
            e.printStackTrace();
        }

ContentResolver cr = this.getContentResolver();
            try {
                if (bmp_selectedPhoto != null)// 如果不釋放的話,不斷取圖片,將會內存不夠
                    bmp_selectedPhoto.recycle();
                bmp_selectedPhoto = BitmapFactory.decodeStream(cr
                        .openInputStream(mUri));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            home_view.setBackground(new BitmapDrawable(getResources(),
                    bmp_selectedPhoto));


 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!