android call system's camera and library examples

  • 2020-05-07 20:26:51
  • OfStack

android mobile phone has its own camera and photo library. We sometimes use to upload pictures to the server in our projects. Today, we did a project to use this function.
First of all, the code of android album and camera is called in the previous section:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// call android Built-in camera  
photoUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
startActivityForResult(intent, 1); 

Intent i = new Intent(Intent.ACTION_PICK, 
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// call android The gallery  
startActivityForResult(i, 2); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
// This method is called when the photo is taken  
super.onActivityResult(requestCode, resultCode, data); 
case 1: 
switch (resultCode) { 
case Activity.RESULT_OK:// Click ok to finish taking photos  
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/pk4fun/"); 
file.mkdirs();//  Create a folder with the name pk4fun //  The name of the photo, under the target folder, with the current time number string as the name, can ensure that each photo name is not the same. Others circulating on the Internet Demo The name of the photo here is dead, and it's going to happen no matter how many photos are taken 1 Zhang always put before 1 Photo overlay. Careful students can also set this string, such as adding the word "IMG"; And then you'll see sd In the card myimage Under this folder, the photo that was taken by the calling camera will be saved, and the name of the photo will not be repeated.  
String str = null; 
Date date = null; 
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");//  Gets the current time, forward 1 The step is converted to a string  
date = new Date(resultCode); 
str = format.format(date); 
String fileName = "/sdcard/myImage/" + str + ".jpg"; 
try { 
b = new FileOutputStream(fileName); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);//  Write the data to a file  
} catch (FileNotFoundException e) { 
} finally { 
try { 
} catch (IOException e) { 
} break; 
case Activity.RESULT_CANCELED://  cancel  
case 2: 
switch (resultCode) { 
case Activity.RESULT_OK: { 
Uri uri = data.getData(); 
Cursor cursor = mActivity.getContentResolver().query(uri, null, 
null, null, null); 
String imgNo = cursor.getString(0); //  Picture number  
String imgPath = cursor.getString(1); //  Image file path  
String imgSize = cursor.getString(2); //  Image size  
String imgName = cursor.getString(3); //  Image file name  
// Options options = new BitmapFactory.Options(); 
// options.inJustDecodeBounds = false; 
// options.inSampleSize = 10; 
// Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options); 
case Activity.RESULT_CANCELED://  cancel  

Finally, remember to add permissions
< uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" / >
< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" / >
< uses-permission android:name="android.permission.CAMERA" / >
< uses-permission android:name="android.permission.RECORD_AUDIO" / >
< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" / >

Related articles: