網站首頁 編程語言 正文
本文實例為大家分享了Android調用手機攝像頭拍照和錄音功能的具體代碼,供大家參考,具體內容如下
調用攝像頭拍照:
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>
調用錄音功能:
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<>();//錄音文件數據源 ? ? 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上,小于則不設置上去 ? ? ? ? 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卡狀態 ? ? ? ? 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() { ? ? ? ? //創建錄音對象 ? ? ? ? recorder=new MediaRecorder(); ? ? ? ? //設置麥克風 ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC); ? ? ? ? //設置轉碼類型 ? ? ? ? recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); ? ? ? ? //設置編碼方式 ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); ? ? ? ? try { ? ? ? ? ? ? //創建錄音文件 ? ? ? ? ? ? recorderFile=File.createTempFile("錄音_",".amr",path); ? ? ? ? ? ? //設置錄音的數據寫到錄音文件里 ? ? ? ? ? ? recorder.setOutputFile(recorderFile.getAbsolutePath()); ? ? ? ? ? ? //錄音準備 ? ? ? ? ? ? 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); ? ? } ? ? ? //調用播放器播放點擊的條目文件 ? ? 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>
原文鏈接:https://blog.csdn.net/Daisuki_ch/article/details/52692957
相關推薦
- 2022-08-30 啟動Activity但是不顯示界面
- 2023-01-01 Pytest使用logging模塊寫日志的實例詳解_python
- 2022-05-11 使用kettle的數據庫增量備份與全量備份
- 2022-08-06 WinForm項目中添加幫助文檔功能_C#教程
- 2023-07-10 Bean的生命周期和作用域
- 2023-07-05 React解決setState異步帶來的多次修改合一和修改后立即使用沒有變化問題
- 2022-06-23 基于Flutter制作一個心碎動畫特效_Android
- 2022-04-25 T-SQL查詢為何慎用IN和NOT?IN詳解_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支