In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how Android invokes the camera photo and recording function of the mobile phone". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to call the camera camera and recording function of the mobile phone in Android" can help you solve your doubts.
Call the camera to take photos:
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);}
Layout file
Call the recording function:
Public class Main2Activity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener {private ListView listView;// recording file control private Button btn1,btn2;// start button and stop button private MediaRecorder recorder;// recording object private List list=new ArrayList (); / / recording file data source private File path,recorderFile;// root directory, the recording file private ArrayAdapter adapter to be stored in the SD card / / Adapter @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main2); init (); if (nulled recording path) {musicList ();}} / / get all recording files private void musicList () {File home=path at initialization / / determine whether the length of the file filter is greater than 0. If it is larger than 0, it will be adapted to listview. If it is less than 0, it will not be 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 (); / / get the root directory} private File getPath () {File file=null; / / determine the SD card status if (Environment.getExternalStorageState (). Equals (Environment.MEDIA_MOUNTED)) {file=Environment.getExternalStorageDirectory () } else {Toast.makeText (this, "No SD card", Toast.LENGTH_SHORT). Show ();} return file;} @ Override public void onClick (View view) {switch (view.getId ()) {/ / start button case R.id.start: startRecorder (); btn1.setEnabled (false) Btn2.setEnabled (true); break; / / stop button case R.id.stop: stopRecorder (); btn1.setEnabled (true); btn2.setEnabled (false); break }} private void stopRecorder () {/ / if the recording file is not null if (recorderFilerecording null) {/ / stop recording recorder.stop (); / / add the recording file name to the collection list.add (recorderFile.getName ()) If (adapters created null) {/ / refresh adapter adapter.notifyDataSetChanged ();} / / release recording object recorder.release (); recorder=null;}} private void startRecorder () {/ / create recording object recorder=new MediaRecorder () / / set microphone recorder.setAudioSource (MediaRecorder.AudioSource.MIC); / / set transcoding type recorder.setOutputFormat (MediaRecorder.OutputFormat.THREE_GPP); / / set encoding mode recorder.setAudioEncoder (MediaRecorder.AudioEncoder.AMR_NB); try {/ / create recording file recorderFile=File.createTempFile ("recording _", ".amr", path) / / set the recording data to be written to the recording file recorder.setOutputFile (recorderFile.getAbsolutePath ()); / / prepare the recording recorder.prepare (); / / start the recording recorder.start ();} catch (IOException e) {e.printStackTrace () } @ Override public void onItemClick (AdapterView adapterView, View view, int I, long l) {/ / get the path of the clicked entry File file=new File (path.getAbsolutePath () + File.separator+list.get (I)); playMusic (file) } / / call the player to play the clicked entry 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);}}
File filtering code:
Public class MusicFilter implements FilenameFilter {@ Override public boolean accept (File file, String name) {return (name.endsWith (".amr");}}
Layout file:
After reading this, the article "how to use Android to take pictures and record videos on mobile cameras" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.