废话不多说 程序贴上
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/open_camera"
android:id="@+id/openCamera"/>
<TextView android:id="@+id/phone_call"
android:text="@string/make_call"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="phone"
android:background="@android:color/white"/>
<TextView android:id="@+id/web_open"
android:text="@string/make_web"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:background="@android:color/white"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/cameraImage"
android:contentDescription="@string/open_camera"/>
</LinearLayout>
package com.yunfeng.bus;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.kobjects.base64.Base64;
import com.yunfeng.bus.dao.ImageUploadDao;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
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;
public class SaySelfActivity extends Activity implements OnClickListener{
private Button openCamera;
private ImageView cameraImage;
private TextView phoneText;
final int RESULT_LOAD_IMAGE = 0;//表示打开系统图库
final int TAKE_PICTURE = 1;//为了表示返回方法中辨识你的程序打开的相机
private Bitmap myBitmap;
private byte[] mContent;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.say_self);
openCamera = (Button)findViewById(R.id.openCamera);
cameraImage = (ImageView)findViewById(R.id.cameraImage);
phoneText = (TextView)findViewById(R.id.phone_call);
phoneText.setOnClickListener(this);
openCamera.setOnClickListener(this);
}
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.phone_call:
/*
* 隐藏的方法是直接拨打电话 用autoink为将电话拨打状态
*/
//Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"));
//startActivity(intent);
break;
case R.id.openCamera:{
final CharSequence[] items =
{ "相册", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(SaySelfActivity.this).setTitle("选择图片").setItems(items,
new DialogInterface.OnClickListener()
{
public void onClick ( DialogInterface dialog , int item )
{
// 这里item是根据选择的方式,
// 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法
if (item == 1){
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, TAKE_PICTURE);
} else{
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg");
startActivityForResult(getImage, 0);
}
}
}).create();
dlg.show();
}
break;
}
}
protected void onActivityResult ( int requestCode , int resultCode , Intent data )
{
super.onActivityResult(requestCode, resultCode, data);
ContentResolver resolver = getContentResolver();
/**
* 因为两种方式都用到了startActivityForResult方法,
* 这个方法执行完后都会执行onActivityResult方法, 所以为了区别到底选择了那个方式获取图片要进行判断,
* 这里的requestCode跟startActivityForResult里面第二个参数对应
*/
if (requestCode == RESULT_LOAD_IMAGE)
{
try
{
Log.v("how", "jhhh");
// 获得图片的uri
Uri originalUri = data.getData();
// 将图片内容解析成字节数组
mContent = readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
Log.v("byte", String.valueOf(mContent));
// 将字节数组转换为ImageView可调用的Bitmap对象
myBitmap = getPicFromBytes(mContent, null);
//上传图片
// //把得到的图片绑定在控件上显示
cameraImage.setImageBitmap(myBitmap);
String result = null;
ImageUploadDao dao = new ImageUploadDao();
result = dao.getImage(mContent);
phoneText.setText(result);
} catch ( Exception e ){
System.out.println(e.getMessage());
}
} if (resultCode == TAKE_PICTURE) {
Bitmap bm = (Bitmap) data.getExtras().get("data");
cameraImage.setImageBitmap(bm);//想图像显示在ImageView视图上,private ImageView img;
File myCaptureFile = new File("sdcard/123456.jpg");
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
/* 采用压缩转档方法 */
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
/* 调用flush()方法,更新BufferStream */
bos.flush();
/* 结束OutputStream */
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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 inStream ) throws Exception
{
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1)
{
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
}