最近寫項目,遇到了,在此記錄一下。Android-ImageView顯示選擇本地圖片或拍照.
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.ContentResolver;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
-
- public class PicChooserActivity extends Activity {
- /** Called when the activity is first created. */
- private ImageView imageView;
- private OnClickListener imgViewListener;
- private Bitmap myBitmap;
- private byte[] mContent;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- imageView =(ImageView) findViewById(R.id.ivPic);
-
- imageView.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- final CharSequence[] items = { "相冊", "拍照" };
-
- AlertDialog dlg = new AlertDialog.Builder(PicChooserActivity.this).setTitle("選擇照片").setItems(items,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- //這裡item是根據選擇的方式, 在items數組裡面定義了兩種方式,拍照的下標為1所以就調用拍照方法
- if(which==1){
- Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
- startActivityForResult(getImageByCamera, 1);
- }else{
- Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
- getImage.addCategory(Intent.CATEGORY_OPENABLE);
- getImage.setType("image/jpeg");
- startActivityForResult(getImage, 0);
- }
-
- }
- }).create();
- dlg.show();
- }
- });
-
- }
-
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, data);
-
- ContentResolver contentResolver =getContentResolver();
- /**
- * 因為兩種方式都用到了startActivityForResult方法,這個方法執行完後都會執行onActivityResult方法,
- * 所以為了區別到底選擇了那個方式獲取圖片要進行判斷,這裡的requestCode跟startActivityForResult裡面第二個參數對應
- */
-
- if(requestCode==0){
-
- //方式一
- /*try {
- //獲得圖片的uri
- Uri orginalUri = data.getData();
- //將圖片內容解析成字節數組
- mContent = readStream(contentResolver.openInputStream(Uri.parse(orginalUri.toString())));
- //將字節數組轉換為ImageView可調用的Bitmap對象
- myBitmap =getPicFromBytes(mContent,null);
- ////把得到的圖片綁定在控件上顯示
- imageView.setImageBitmap(myBitmap);
- } catch (Exception e) {
- e.printStackTrace();
- // TODO: handle exception
- }*/
-
- //方式二
- try {
- Uri selectedImage = data.getData();
- String[] filePathColumn = { MediaStore.Images.Media.DATA };
-
- Cursor cursor = getContentResolver().query(selectedImage,
- filePathColumn, null, null, null);
- cursor.moveToFirst();
-
- int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
- String picturePath = cursor.getString(columnIndex);
- cursor.close();
- imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
-
-
- }else if(requestCode==1){
- try {
- Bundle extras = data.getExtras();
- myBitmap = (Bitmap) extras.get("data");
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos);
- mContent=baos.toByteArray();
- } catch (Exception e) {
- e.printStackTrace();
- // TODO: handle exception
- }
- imageView.setImageBitmap(myBitmap);
- }
-
- }
-
- public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
- if (bytes != null)
- if (opts != null)
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);
- else
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- return null;
- }
-
-
-
- public static byte[] readStream(InputStream in) throws Exception{
- byte[] buffer =new byte[1024];
- int len =-1;
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-
- while((len=in.read(buffer))!=-1){
- outStream.write(buffer, 0, len);
- }
- byte[] data =outStream.toByteArray();
- outStream.close();
- in.close();
- return data;
- }
-
- }
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11