The Android instance explains how to add local images and call the system to take photos.
In the project development process, we can't do without pictures. Sometimes we need to call local images, and sometimes we need to call pictures. At the same time, there are two ways to take pictures. One is to call the System camera function, and the other is to customize the camera function. This blog post only describes the first method and the second method later.
To add a local image and call the system to take a photo, call the acitivity method to jump to startActivityForResult (Intent intent, int requestCode) and the onActivityResult (int requestCode, int resultCode, Intent data) returned by the activity) method. The specific implementation code is as follows:
1. Add a local image
1.
Intent intent = new Intent ();/* enable Pictures image Type to set to image */intent. setType (IMAGE_TYPE);/* use Intent. ACTION_GET_CONTENT this Action */intent. setAction (Intent. ACTION_GET_CONTENT);/* returns the current screen after obtaining the photo */startActivityForResult (intent, LOCAL_IMAGE_CODE );
2.
Uri uri = data. getData (); url = uri. toString (). substring (uri. toString (). indexOf ("//") + 2); if (url. contains (". jpg ") & url. contains (". png ") {Toast. makeText (this, "select image", Toast. LENGTH_SHORT ). show (); return;} bitmap = HelpUtil. getBitmapByUrl (url );
Ii. Call the system to take pictures
1.
String fileName = "IMG_" + curFormatDateStr + ".png";Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(rootUrl, fileName)));intent.putExtra("fileName", fileName);startActivityForResult(intent, CAMERA_IMAGE_CODE);
2.
url = rootUrl + "/" + "IMG_" + curFormatDateStr + ".png";bitmap = HelpUtil.getBitmapByUrl(url);showImageIv.setImageBitmap(HelpUtil.createRotateBitmap(bitmap));
Note: Because the obtained image is automatically rotated 90 degrees counterclockwise in ImageView, it must be rotated 90 degrees clockwise to reach the normal display level. The method is as follows:
/*** Bitmap Rotate 90 degrees ** @ param bitmap * @ return */public static Bitmap createRotateBitmap (Bitmap bitmap) {if (bitmap! = Null) {Matrix m = new Matrix (); try {m. setRotate (90, bitmap. getWidth ()/2, bitmap. getHeight ()/2); // 90 is the 90-degree Bitmap BMP 2 = Bitmap we need to select. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), m, true); bitmap. recycle (); bitmap = BMP 2;} catch (Exception ex) {System. out. print ("An error occurred while creating the image! "+ Ex) ;}} return bitmap ;}
3. The instance provides detailed code for the entire effect.
1.
2. layout file activity_main.xml
3. Main class file MainActivity. java
Package com. example. insertimagedemo; import java. io. file; import java. util. calendar; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android.net. uri; 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; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private static final int LOCAL_IMAGE_CODE = 1; private static final int CAMERA_IMAGE_CODE = 2; private static final String IMAGE_TYPE = "image /*"; private String rootUrl = null; private String curFormatDateStr = null; private Button localImgBtn, cameraImgBtn; private TextView showUrlTv; private ImageView showImageIv; @ Overrideprotected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findById (); initData ();}/*** initialize view */private void findById () {localImgBtn = (Button) this. findViewById (R. id. id_local_img_btn); cameraImgBtn = (Button) this. findViewById (R. id. id_camera_img_btn); showUrlTv = (TextView) this. findViewById (R. id. id_show_url_ TV); showImageIv = (ImageView) this. findViewById (R. id. id_image_iv); localImgBtn. setOnClickListener (this); cameraImgBtn. setOnClickListener (this);}/*** initialize related data */private void initData () {rootUrl = Environment. getExternalStorageDirectory (). getPath () ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. id_local_img_btn: processLocal (); break; case R. id. id_camera_img_btn: processCamera (); break ;}/ *** process btn events on a local image */private void processLocal () {Intent intent = new Intent (); /* enable Pictures to set the image Type to image */intent. setType (IMAGE_TYPE);/* use Intent. ACTION_GET_CONTENT this Action */intent. setAction (Intent. ACTION_GET_CONTENT);/* returns the current image after obtaining the photo */startActivityForResult (intent, LOCAL_IMAGE_CODE);}/*** processes the camera image btn event */private void processCamera () {curFormatDateStr = HelpUtil. getDateFormatString (Calendar. getInstance (). getTime (); String fileName = "IMG _" + curFormatDateStr + ". png "; Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (new File (rootUrl, fileName); intent. putExtra ("fileName", fileName); startActivityForResult (intent, CAMERA_IMAGE_CODE);}/*** return event after Activity jump Processing */@ Overridepublic void onActivityResult (int requestCode, int resultCode, intent data) {if (resultCode = RESULT_ OK) {String url = ""; Bitmap bitmap = null; if (requestCode = LOCAL_IMAGE_CODE) {Uri uri = data. getData (); url = uri. toString (). substring (uri. toString (). indexOf ("//") + 2); Log. e ("uri", uri. toString (); if (url. contains (". jpg ") & url. contains (". png ") {Toast. makeText (this, "select image", Toast. LENGTH_SHORT ). show (); return;} bitmap = HelpUtil. getBitmapByUrl (url); showImageIv. setImageBitmap (HelpUtil. getBitmapByUrl (url);/*** obtain bitmap another method ** ContentResolver cr = this. getContentResolver (); bitmap = * HelpUtil. getBitmapByUri (uri, cr); */} else if (requestCode = CAMERA_IMAGE_CODE) {url = rootUrl + "/" + "IMG _" + curFormatDateStr + ". png "; bitmap = HelpUtil. getBitmapByUrl (url); showImageIv. setImageBitmap (HelpUtil. createRotateBitmap (bitmap);/*** obtain bitmap another method ** File picture = new File (url); * Uri uri = Uri. fromFile (picture); * ContentResolver cr = this. getContentResolver (); * bitmap = HelpUtil. getBitmapByUri (uri, cr); */} showUrlTv. setText (url);} else {Toast. makeText (this, "no image added", Toast. LENGTH_SHORT ). show ();}}}
4. Help class file HelpUtil. java
Package com. example. insertimagedemo; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; import java. text. simpleDateFormat; import java. util. date; import android. annotation. suppressLint; import android. content. contentResolver; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. matrix; import android.net. uri; public class HelpUtil {/*** obtain the Bitmap of the Local Image Based on the image path ** @ param url * @ return */public static Bitmap getBitmapByUrl (String url) {FileInputStream FCM = null; bitmap bitmap = null; try {FS = new FileInputStream (url); bitmap = BitmapFactory. decodeStream (FCM);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace (); bitmap = null;} finally {if (FS! = Null) {try {FS. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} fiis = null;} return bitmap ;} /*** bitmap Rotate 90 degrees ** @ param bitmap * @ return */public static Bitmap createRotateBitmap (Bitmap bitmap) {if (bitmap! = Null) {Matrix m = new Matrix (); try {m. setRotate (90, bitmap. getWidth ()/2, bitmap. getHeight ()/2); // 90 is the 90-degree Bitmap BMP 2 = Bitmap we need to select. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), m, true); bitmap. recycle (); bitmap = BMP 2;} catch (Exception ex) {System. out. print ("An error occurred while creating the image! "+ Ex) ;}} return bitmap;} public static Bitmap getBitmapByUri (Uri uri, ContentResolver cr) {Bitmap bitmap = null; try {bitmap = BitmapFactory. decodeStream (cr. openInputStream (uri);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace (); bitmap = null;} return bitmap;}/*** get the formatted date string * @ param date * @ return */@ SuppressLint ("SimpleDateFormat ") public static String getDateFormatString (Date date) {if (date = null) date = new Date (); String formatStr = new String (); simpleDateFormat matter = new SimpleDateFormat ("yyyyMMdd_HHmmss"); formatStr = matter. format (date); return formatStr ;}}
I used to read all the content in this blog. Thank you for reading this article.
Source Code address: http://download.csdn.net/detail/a123demi/8027697