Android implements the method by calling the camera and storing the photo on the sd card

  • 2020-05-07 20:25:23
  • OfStack

There are two ways to take photos in Android. One is to call the camera that comes with the system and use the photo data it returns. There is also one is to use Camera class and other related classes to achieve camera function, this method is relatively high system, washing and dyeing is also more complex, 1 common applications only need to use the first can.
starts the camera with Intent code :
 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, 1); After the photo is taken onActivityResult(int requestCode, int resultCode, Intent data) Get to the Bitmap The object. Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 

It's a good idea to check 1 to see if the sd card is available before storing the image on the sd card
 
String sdStatus = Environment.getExternalStorageState(); 
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { //  detection sd Whether the available  
Log.v("TestFile", 
"SD card is not avaiable/writeable right now."); 
return; 
} 

The following code can achieve the image file saved in the "sdcard/myImage/" folder, the name is "111.jpg"
 
File file = new File("/sdcard/myImage/"); 
file.mkdirs();//  Create folder  
String fileName = "/sdcard/myImage/111.jpg"; 
try { 
b = new FileOutputStream(fileName); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);//  Write the data to a file  
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} finally { 
try { 
b.flush(); 
b.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 

Also note that to read or write sd carvings you must first configure permissions in the Mainifest.xml file:
 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 

1 demo, achieve call system camera to take a picture, it will be displayed on the screen, and save to sd card.
The full code is as follows:
MyCaremaActivity.java
 
package barry.android.c; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
public class MyCaremaActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
Button button = (Button) findViewById(R.id.button); 
button.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
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 == Activity.RESULT_OK) { 
String sdStatus = Environment.getExternalStorageState(); 
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { //  detection sd Whether the available  
Log.v("TestFile", 
"SD card is not avaiable/writeable right now."); 
return; 
} 
Bundle bundle = data.getExtras(); 
Bitmap bitmap = (Bitmap) bundle.get("data");//  Gets the data returned by the camera and converts to Bitmap Image format  
FileOutputStream b = null; 
File file = new File("/sdcard/myImage/"); 
file.mkdirs();//  Create folder  
String fileName = "/sdcard/myImage/111.jpg"; 
try { 
b = new FileOutputStream(fileName); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);//  Write the data to a file  
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} finally { 
try { 
b.flush(); 
b.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);//  Show the picture in ImageView In the  
} 
} 
} 

main.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<Button 
android:id="@+id/button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text=" Click on start camera " /> 
<ImageView 
android:id="@+id/imageView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#999999" /> 
</LinearLayout> 

AndroidMainifest.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="barry.android.c" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-sdk android:minSdkVersion="7" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
<application 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" > 
<activity 
android:label="@string/app_name" 
android:name=".MyCaremaActivity" > 
<intent-filter > 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
</activity> 
</application> 
</manifest> 

Related articles: