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

Android調(diào)用手機攝像頭拍照和錄音功能

 更新時間:2022年03月28日 16:43:55   作者:Daisuki_ch  
這篇文章主要為大家詳細介紹了Android調(diào)用手機攝像頭拍照和錄音功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android調(diào)用手機攝像頭拍照和錄音功能的具體代碼,供大家參考,具體內(nèi)容如下

調(diào)用攝像頭拍照:

public class MainActivity extends Activity {
?
? ? private Button button;
? ? private ImageView imageView;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? imageView= (ImageView) findViewById(R.id.imageView);
? ? ? ? button= (Button) findViewById(R.id.btn);
? ? ? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
? ? ? ? ? ? ? ? startActivityForResult(intent,1);
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
? ? @Override
? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
? ? ? ? super.onActivityResult(requestCode, resultCode, data);
? ? ? ? if(resultCode==RESULT_OK){
? ? ? ? ? ? Bundle bundle=data.getExtras();
? ? ? ? ? ? Bitmap bitmap= (Bitmap) bundle.get("data");
? ? ? ? ? ? if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
? ? ? ? ? ? ? ? File file=new File(Environment.getExternalStorageDirectory(),"MyImage");
? ? ? ? ? ? ? ? if(!file.exists()){
? ? ? ? ? ? ? ? ? ? file.mkdir();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? String date=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
? ? ? ? ? ? ? ? ? ? String path=file+"/"+date+".jpg";
? ? ? ? ? ? ? ? ? ? FileOutputStream outputStream=new FileOutputStream(path);
? ? ? ? ? ? ? ? ? ? bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
? ? ? ? ? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? imageView.setImageBitmap(bitmap);
? ? ? ? }
? ? }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:orientation="vertical"
? ? android:gravity="center"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
>
?
? ? <Button
? ? ? ? android:id="@+id/btn"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="Hello World!" />
? ? <ImageView
? ? ? ? android:id="@+id/imageView"
? ? ? ? android:layout_width="200dp"
? ? ? ? android:layout_height="200dp" />
</LinearLayout>

調(diào)用錄音功能:

public class Main2Activity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{
?
? ? private ListView listView;//錄音文件控件
? ? private Button btn1,btn2;//開始按鈕和停止按鈕
? ? private MediaRecorder recorder;//錄音對象
? ? private List<String> list=new ArrayList<>();//錄音文件數(shù)據(jù)源
? ? private File path,recorderFile;//根目錄,要存入sd卡的錄音文件
? ? private ArrayAdapter adapter;//適配器
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main2);
? ? ? ? init();
? ? ? ? if(null!=path){
? ? ? ? ? ? musicList();
? ? ? ? }
? ? }
?
? ? //初始化時獲得所有錄音文件
? ? private void musicList() {
? ? ? ? File home=path;
? ? ? ? //判斷文件過濾器的長度是否大于0,大于則適配到listview上,小于則不設(shè)置上去
? ? ? ? if(home.listFiles(new MusicFilter()).length>0){
? ? ? ? ? ? for(File file:home.listFiles(new MusicFilter())){
? ? ? ? ? ? ? ? list.add(file.getName());
? ? ? ? ? ? }
? ? ? ? ? ? adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
? ? ? ? ? ? listView.setAdapter(adapter);
? ? ? ? }
? ? }
?
? ? private void init() {
? ? ? ? listView= (ListView) findViewById(R.id.listView);
? ? ? ? listView.setOnItemClickListener(this);
? ? ? ? btn1= (Button) findViewById(R.id.start);
? ? ? ? btn2= (Button) findViewById(R.id.stop);
? ? ? ? btn1.setOnClickListener(this);
? ? ? ? btn2.setOnClickListener(this);
? ? ? ? path=getPath();//獲得根目錄
? ? }
?
? ? private File getPath() {
? ? ? ? File file=null;
? ? ? ? //判斷sd卡狀態(tài)
? ? ? ? if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
? ? ? ? ? ? file=Environment.getExternalStorageDirectory();
? ? ? ? }else{
? ? ? ? ? ? Toast.makeText(this,"沒有SD卡",Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? ? ? return file;
? ? }
?
? ? @Override
? ? public void onClick(View view) {
? ? ? ? switch (view.getId()){
? ? ? ? ? ? //開始按鈕
? ? ? ? ? ? case R.id.start:
? ? ? ? ? ? ? ? startRecorder();
? ? ? ? ? ? ? ? btn1.setEnabled(false);
? ? ? ? ? ? ? ? btn2.setEnabled(true);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? //停止按鈕
? ? ? ? ? ? case R.id.stop:
? ? ? ? ? ? ? ? stopRecorder();
? ? ? ? ? ? ? ? btn1.setEnabled(true);
? ? ? ? ? ? ? ? btn2.setEnabled(false);
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
?
? ? private void stopRecorder() {
? ? ? ? //如果錄音的文件不為null
? ? ? ? if(recorderFile!=null){
? ? ? ? ? ? //停止錄音
? ? ? ? ? ? recorder.stop();
? ? ? ? ? ? //把錄音文件的名字加入集合里
? ? ? ? ? ? list.add(recorderFile.getName());
? ? ? ? ? ? if(adapter!=null){
? ? ? ? ? ? ? ? //刷新適配器
? ? ? ? ? ? ? ? adapter.notifyDataSetChanged();
? ? ? ? ? ? }
? ? ? ? ? ? //釋放錄音對象
? ? ? ? ? ? recorder.release();
? ? ? ? ? ? recorder=null;
? ? ? ? }
?
? ? }
?
? ? private void startRecorder() {
? ? ? ? //創(chuàng)建錄音對象
? ? ? ? recorder=new MediaRecorder();
? ? ? ? //設(shè)置麥克風(fēng)
? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
? ? ? ? //設(shè)置轉(zhuǎn)碼類型
? ? ? ? recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
? ? ? ? //設(shè)置編碼方式
? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
? ? ? ? try {
? ? ? ? ? ? //創(chuàng)建錄音文件
? ? ? ? ? ? recorderFile=File.createTempFile("錄音_",".amr",path);
? ? ? ? ? ? //設(shè)置錄音的數(shù)據(jù)寫到錄音文件里
? ? ? ? ? ? recorder.setOutputFile(recorderFile.getAbsolutePath());
? ? ? ? ? ? //錄音準(zhǔn)備
? ? ? ? ? ? recorder.prepare();
? ? ? ? ? ? //錄音開始
? ? ? ? ? ? recorder.start();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
?
? ? @Override
? ? public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
? ? ? ? //獲得點擊條目的路徑
? ? ? ? ? ? File file=new File(path.getAbsolutePath()+File.separator+list.get(i));
? ? ? ? ? ? playMusic(file);
? ? }
?
? ? //調(diào)用播放器播放點擊的條目文件
? ? private void playMusic(File file) {
? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
? ? ? ? Uri uri = Uri.fromFile(file);
? ? ? ? intent.setDataAndType(uri, "audio/mp3");
? ? ? ? startActivity(intent);
? ? }
}

文件過濾代碼:

public class MusicFilter implements FilenameFilter {
? ? @Override
? ? public boolean accept(File file, String name) {
? ? ? ? return (name.endsWith(".amr"));
? ? }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:orientation="vertical"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent">
?
? ? <Button
? ? ? ? android:id="@+id/start"
? ? ? ? android:text="開始錄音"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content" />
? ? <Button
? ? ? ? android:id="@+id/stop"
? ? ? ? android:text="停止錄音"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content" />
? ? <ListView
? ? ? ? android:id="@+id/listView"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"></ListView>
?
</LinearLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Kotlin 中范圍操作符的使用示例代碼(范圍操作符不同用法)

    Kotlin 中范圍操作符的使用示例代碼(范圍操作符不同用法)

    Kotlin中的范圍操作符(in、step、downTo、until)在Android開發(fā)中非常實用,通過這些操作符,可以簡潔地遍歷整數(shù)范圍,實現(xiàn)各種遍歷需求,提高代碼的可讀性和可維護性,本文介紹Kotlin 中范圍操作符的使用示例,感興趣的朋友一起看看吧
    2025-03-03
  • Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié)

    Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié)

    這篇文章主要介紹了Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié),Fragment的存在是為了解決不同屏幕分辯率的動態(tài)和靈活UI設(shè)計,需要的朋友可以參考下
    2016-02-02
  • Android仿微信activity滑動關(guān)閉效果

    Android仿微信activity滑動關(guān)閉效果

    這篇文章主要為大家詳細介紹了Android仿微信activity滑動關(guān)閉的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android底部導(dǎo)航組件BottomNavigationView

    Android底部導(dǎo)航組件BottomNavigationView

    這篇文章主要介紹了Android底部導(dǎo)航組件BottomNavigationView,BottomNavigationView是相當(dāng)于一個導(dǎo)航的標(biāo)簽,但是它的形式就是像QQ微信之類的界面,至于寫出后怎樣綁定這三個界面,就得用Fragment,寫這三個頁面的布局
    2023-03-03
  • Eclipse新建Android項目報錯解決方案詳細匯總

    Eclipse新建Android項目報錯解決方案詳細匯總

    這篇文章主要介紹了Eclipse新建Android項目報錯解決方案詳細匯總,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Android編程實現(xiàn)下載圖片及在手機中展示的方法

    Android編程實現(xiàn)下載圖片及在手機中展示的方法

    這篇文章主要介紹了Android編程實現(xiàn)下載圖片及在手機中展示的方法,涉及Android針對圖形文件的遠程下載及遍歷顯示相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • Android圖片加載利器之Picasso擴展功能

    Android圖片加載利器之Picasso擴展功能

    這篇文章主要為大家詳細介紹了Android圖片加載利器之Picasso擴展功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android自定義UI手勢密碼簡單版

    Android自定義UI手勢密碼簡單版

    這篇文章主要為大家詳細介紹了Android自定義UI手勢密碼簡單版
    2016-10-10
  • Android實戰(zhàn)教程第八篇之短信備份

    Android實戰(zhàn)教程第八篇之短信備份

    這篇文章主要為大家詳細介紹了Android實戰(zhàn)教程第八篇之短信備份的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android 開發(fā)線程的分析

    Android 開發(fā)線程的分析

    這篇文章主要介紹了Android 開發(fā)線程的分析的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論

宣威市| 漯河市| 同德县| 亳州市| 怀化市| 绥滨县| 台州市| 靖西县| 定结县| 陵川县| 巴彦县| 南宫市| 成安县| 康马县| 马关县| 辰溪县| 仙桃市| 襄汾县| 安丘市| 北安市| 宝兴县| 藁城市| 新津县| 乡宁县| 岫岩| 宜兰市| 福州市| 湘西| 鹰潭市| 龙南县| 凯里市| 安化县| 祥云县| 视频| 房产| 安达市| 曲麻莱县| 北票市| 天津市| 天气| 龙泉市|