最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 調(diào)用系統(tǒng)相機(jī)拍攝獲取照片的兩種方法實(shí)現(xiàn)實(shí)例

 更新時(shí)間:2016年11月30日 15:00:02   投稿:lqh  
這篇文章主要介紹了Android 調(diào)用系統(tǒng)相機(jī)拍攝獲取照片的兩種方法實(shí)現(xiàn)實(shí)例的相關(guān)資料,一種是通過Bundle來獲取壓縮過的照片,一種是通過SD卡獲取的原圖,需要的朋友可以參考下

Android 調(diào)用系統(tǒng)相機(jī)拍攝獲取照片的兩種方法實(shí)現(xiàn)實(shí)例

在我們Android開發(fā)中經(jīng)常需要做這個(gè)一個(gè)功能,調(diào)用系統(tǒng)相機(jī)拍照,然后獲取拍攝的照片。下面是我總結(jié)的兩種方法獲取拍攝之后的照片,一種是通過Bundle來獲取壓縮過的照片,一種是通過SD卡獲取的原圖。

下面是演示代碼:

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  android:gravity="center_horizontal"  
  > 
 
  <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="拍照獲取縮略圖" /> 
 
  <Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="14dp" 
    android:text="拍照獲取原圖" /> 
 
  <ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:src="@drawable/ic_launcher" /> 
 
</LinearLayout> 

java代碼:



package com.example.cameardemo; 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.support.v7.app.ActionBarActivity; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
 
public class MainActivity extends ActionBarActivity implements OnClickListener { 
 
  private static int REQUEST_THUMBNAIL = 1;// 請求縮略圖信號(hào)標(biāo)識(shí) 
  private static int REQUEST_ORIGINAL = 2;// 請求原圖信號(hào)標(biāo)識(shí) 
  private Button button1, button2; 
  private ImageView mImageView; 
  private String sdPath;//SD卡的路徑 
  private String picPath;//圖片存儲(chǔ)路徑 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_main); 
 
    button1 = (Button) findViewById(R.id.button1); 
    button2 = (Button) findViewById(R.id.button2); 
    mImageView = (ImageView) findViewById(R.id.imageView1); 
 
    button1.setOnClickListener(this); 
    button2.setOnClickListener(this); 
    //獲取SD卡的路徑 
    sdPath = Environment.getExternalStorageDirectory().getPath(); 
    picPath = sdPath + "/" + "temp.png"; 
    Log.e("sdPath1",sdPath); 
  } 
 
  @Override 
  public void onClick(View view) { 
    switch (view.getId()) { 
    case R.id.button1://第一種方法,獲取壓縮圖 
      Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      // 啟動(dòng)相機(jī) 
      startActivityForResult(intent1, REQUEST_THUMBNAIL); 
      break; 
 
    case R.id.button2://第二種方法,獲取原圖 
      Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      Uri uri = Uri.fromFile(new File(picPath)); 
      //為拍攝的圖片指定一個(gè)存儲(chǔ)的路徑 
      intent2.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
      startActivityForResult(intent2, REQUEST_ORIGINAL); 
      break; 
 
    default: 
      break; 
    } 
  } 
 
  /** 
   * 返回應(yīng)用時(shí)回調(diào)方法 
   */ 
  @Override 
  protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
 
    if (resultCode == RESULT_OK) { 
      if (requestCode == REQUEST_THUMBNAIL) {//對應(yīng)第一種方法 
        /** 
         * 通過這種方法取出的拍攝會(huì)默認(rèn)壓縮,因?yàn)槿绻鄼C(jī)的像素比較高拍攝出來的圖會(huì)比較高清, 
         * 如果圖太大會(huì)造成內(nèi)存溢出(OOM),因此此種方法會(huì)默認(rèn)給圖片盡心壓縮 
         */ 
        Bundle bundle = data.getExtras(); 
        Bitmap bitmap = (Bitmap) bundle.get("data"); 
        mImageView.setImageBitmap(bitmap); 
      } 
      else if(resultCode == REQUEST_ORIGINAL){//對應(yīng)第二種方法 
        /** 
         * 這種方法是通過內(nèi)存卡的路徑進(jìn)行讀取圖片,所以的到的圖片是拍攝的原圖 
         */ 
        FileInputStream fis = null; 
        try { 
          Log.e("sdPath2",picPath); 
          //把圖片轉(zhuǎn)化為字節(jié)流 
          fis = new FileInputStream(picPath); 
          //把流轉(zhuǎn)化圖片 
          Bitmap bitmap = BitmapFactory.decodeStream(fis); 
          mImageView.setImageBitmap(bitmap); 
        } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
        }finally{ 
          try { 
            fis.close();//關(guān)閉流 
          } catch (IOException e) { 
            e.printStackTrace(); 
          } 
        } 
      } 
    } 
  } 
 
} 

最后不要忘記在清單文件上添加上讀取SD卡的權(quán)限:



<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.example.cameardemo" 
  android:versionCode="1" 
  android:versionName="1.0" > 
 
  <uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="19" /> 
  <!-- 操作sd卡的權(quán)限 --> 
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
   
  <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
      android:name="com.example.cameardemo.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
       
      <!-- 具有相機(jī)功能 --> 
      <intent-filter > 
        <action android:name="android.media.action.IMAGE_CAPTURE"/> 
         
        <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
    </activity> 
  </application> 
 
</manifest> 

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Android網(wǎng)絡(luò)開發(fā)中GET與POST請求詳解

    Android網(wǎng)絡(luò)開發(fā)中GET與POST請求詳解

    這篇文章主要介紹了android實(shí)現(xiàn)網(wǎng)絡(luò)請求的get和post請求的簡單封裝與使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-12-12
  • android通過jxl讀excel存入sqlite3數(shù)據(jù)庫

    android通過jxl讀excel存入sqlite3數(shù)據(jù)庫

    本文主要介紹了android通過jxl去讀excel的內(nèi)容,然后存入sqlite3數(shù)據(jù)庫表,需要用到j(luò)xl的jar包和sqlite 的jar包,圖片是excel的數(shù)據(jù)格式,需要的朋友可以參考下
    2014-03-03
  • Android邊框裁切的正確姿勢實(shí)現(xiàn)示例

    Android邊框裁切的正確姿勢實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了Android邊框裁切的正確姿勢實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Android實(shí)現(xiàn)雙模(CDMA/GSM)手機(jī)短信監(jiān)聽的方法

    Android實(shí)現(xiàn)雙模(CDMA/GSM)手機(jī)短信監(jiān)聽的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)雙模(CDMA/GSM)手機(jī)短信監(jiān)聽的方法,涉及Android短信的原理與相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android仿支付寶的頭部伸縮動(dòng)畫效果

    Android仿支付寶的頭部伸縮動(dòng)畫效果

    Android5.0推出的MaterialDesign庫包含了處理頭部工具欄的多個(gè)控件,不但允許自定義頂部導(dǎo)航欄,而且導(dǎo)航欄高度是可以伸縮的。下文給大家介紹Android仿支付寶的頭部伸縮動(dòng)畫效果,需要的朋友參考下吧
    2017-05-05
  • Android Studio實(shí)現(xiàn)標(biāo)題欄和狀態(tài)欄的隱藏

    Android Studio實(shí)現(xiàn)標(biāo)題欄和狀態(tài)欄的隱藏

    這篇文章主要介紹了Android Studio實(shí)現(xiàn)標(biāo)題欄和狀態(tài)欄的隱藏功能,在文中給大家補(bǔ)充介紹了android studio 去掉標(biāo)題欄狀態(tài)欄的完整代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-11-11
  • Android利用SoundPool實(shí)現(xiàn)音樂池

    Android利用SoundPool實(shí)現(xiàn)音樂池

    這篇文章主要為大家詳細(xì)介紹了Android利用SoundPool實(shí)現(xiàn)音樂池,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Android實(shí)現(xiàn)將View保存成Bitmap的方法

    Android實(shí)現(xiàn)將View保存成Bitmap的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)將View保存成Bitmap的方法,涉及Android畫布Canvas、位圖bitmap及View的相關(guān)使用技巧,需要的朋友可以參考下
    2016-06-06
  • Android實(shí)現(xiàn)折線走勢圖

    Android實(shí)現(xiàn)折線走勢圖

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)折線走勢圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android定制RadioButton樣式三種實(shí)現(xiàn)方法

    Android定制RadioButton樣式三種實(shí)現(xiàn)方法

    三種方法實(shí)現(xiàn)Android定制RadioButton樣式:使用XML文件進(jìn)行定義/在JAVA代碼中定義等等,感興趣的朋友可以參考下,希望可以幫助到你
    2013-02-02

最新評論

阿尔山市| 马山县| 嫩江县| 鄂托克前旗| 双峰县| 虞城县| 博罗县| 建水县| 巴林左旗| 兰考县| 长宁县| 贵阳市| 鹤壁市| 德化县| 南投县| 铅山县| 内丘县| 巴南区| 包头市| 壤塘县| 大城县| 遵义市| 庆阳市| 崇左市| 延边| 沂源县| 台南市| 淮南市| 许昌市| 大同市| 南宫市| 安庆市| 泰来县| 杨浦区| 西峡县| 莱州市| 进贤县| 萝北县| 安新县| 海原县| 滕州市|