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

來源:互聯網
上載者:User

標籤:des   android   io   ar   java   for   檔案   資料   sp   

方法一:

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

/**   啟動Camera */private void intentCamera(){    try {         Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);         startActivityForResult(cameraIntent, 0);     } catch (ActivityNotFoundException e) {         e.printStackTrace();     }}

/**  在onActivityResult中擷取圖片  */private void getImgFromCamera(){    Bundle bundle = data.getExtras();    bm = (Bitmap) bundle.get("data");    if (bm != null)        bm.recycle();    bm = (Bitmap) data.getExtras().get("data");    if(bm != null){        img.setImageBitmap(bm);    }}

方法二:

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

/*** 開啟照相機     */private void intentCamera(){    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);    File file = new File(Environment.getExternalStorageDirectory() + "/Images");    if(!file.exists()){        file.mkdirs();    }    Uri mUri = Uri.fromFile(        new File(Environment.getExternalStorageDirectory() + "/Images/",         "cameraImg" + String.valueOf(System.currentTimeMillis()) + ".jpg"));    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);    cameraIntent.putExtra("return-data", true);    startActivityForResult(cameraIntent, 1);}

/*** 擷取相機返回的資料      */private void getImgFromCamera(){            ContentResolver cr = this.getContentResolver();    try {        if(cameraBitmap != null)             cameraBitmap.recycle();// 如果不釋放的話,不斷取圖片,將會記憶體不夠        cameraBitmap = BitmapFactory.decodeStream(cr.openInputStream(mUri));        if(bm != null){            img.setImageBitmap(bm);        }    } catch (FileNotFoundException e) {         // TODO Auto-generated catch block        Log.e("error", "從相機中擷取圖片失敗=====");        e.printStackTrace();    }}


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.