本文共 3077 字,大约阅读时间需要 10 分钟。
注意:本程序是我自己写的,测试过的,可以直接运行,完全适合新手小白,是非常适合学习的实例,下载地址在下方、
新手需要注意,在AndroidManifest.xml文件中需要添加权限,否则无法程序会报错
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />下载地址:http://download.csdn.net/detail/cf8833/9283371
MainActivity.java
package com.example.zmap;
import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button bn_photo; private Button bn_check; private ImageView iv_photo; private Intent intent; private final int OPEN_RESULT = 1; // 打开照相机 private final int PICK_RESULT = 2; // 选择图片库 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bn_photo = (Button) findViewById(R.id.bn_photo); bn_check = (Button) findViewById(R.id.bn_check); iv_photo = (ImageView) findViewById(R.id.iv_photo); bn_photo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub intent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, OPEN_RESULT); } }); bn_check.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, PICK_RESULT); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case OPEN_RESULT: if (resultCode == RESULT_OK) { Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap)bundle.get("data"); iv_photo.setImageBitmap(bitmap); } break; case PICK_RESULT: if (resultCode == RESULT_OK) { Uri uri = data.getData(); iv_photo.setImageURI(uri); } break; default: break; } } }activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/bn_photo" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="拍照" /> <Button android:id="@+id/bn_check" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="本地" /> </LinearLayout> <ImageView android:id="@+id/iv_photo" android:layout_width="200dp" android:layout_height="200dp" android:layout_marginTop="20dp" android:layout_gravity="center" android:src="@drawable/ic_launcher" /> </LinearLayout>转载地址:http://lspdz.baihongyu.com/