图片上传,包括从相册选取与拍照上传
转自论坛,还没试
类似开心001的照片上传功能,刚做的时候搜了些例子,有的好用有的不好用,最后东拼西凑,乱七八糟的写了一个。有不对的或更好的方法希望大家指点。下面开始贴代码:两个监听事件:
// 拍照上传
private OnClickListener mUploadClickListener = new OnClickListener() {
public void onClick(View v) {
// 调用相机
Intent mIntent = new Intent("android.media.action.IMAGE_CAPTURE");
// 图片存储路径,可自定义
File tmpFile = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
// 获取这个图片的URI
originalUri = Uri.fromFile(tmpFile);//这是个实例变量,方便下面获取图片的时候用
mIntent.putExtra(MediaStore.EXTRA_OUTPUT, originalUri);
startActivityForResult(mIntent, ACTIVITY_IMAGE_CAPTURE);
}
};
// 打开相册
private OnClickListener mPicListClickListener = new OnClickListener() {
public void onClick(View v) {
// 调用相册
Intent mIntent= new Intent(Intent.ACTION_GET_CONTENT);
mIntent.addCategory(Intent.CATEGORY_OPENABLE);
mIntent.setType(MIME_TYPE_IMAGE_JPEG);
startActivityForResult(mIntent, ACTIVITY_GET_IMAGE);
}
};
监听事件写好了,怎么调用不用我说了吧。这是个startActivityForResult事件,对应的我们肯定得有个onActivityResult,贴之
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
Bitmap bm = null;
ContentResolver resolver = getContentResolver();
String filePath = "/sdcard/bengxin/bx_upload_tmp.jpg";//这个是暂存图片的路径
FileOutputStream output = null;
try {
// 创建暂存图片
if (Utils.CreateFile(filePath)) {
output = new FileOutputStream(filePath);
} else {
throw new Exception("内部错误");
}
if (requestCode == ACTIVITY_GET_IMAGE) {
// 获得图片的uri
originalUri = data.getData();
PS:拍照的那个URI我们在上面已经获取了
}
/**** 获取图片开始 ****/
//mContent是上传的图片byte[]数组,得到这个后随便怎么处理,当然你也可以直接用fileInput流
fileInput = (FileInputStream) resolver.openInputStream(Uri
.parse(originalUri.toString()));
// 将图片内容解析成字节数组
mContent = getBytesFromInputStream(fileInput, 3500000);
fileInput.close();
// 将字节数组转换为ImageView可调用的Bitmap对象
bm = getPicFromBytes(mContent, null);
/********* 获取图片完了 ************/
// 将图片缩小到指定比例并且保存到缓存文件
float scale = ((float) 210) / ((float) bm.getWidth());
bm = Utils.smallBmp(bm, scale);//这个缩小功能是自己写的方法
//将Bitmap读到文件中去,注意这个是压缩,那个100是压缩比,0-100,越大质量越好
bm.compress(CompressFormat.JPEG, 100, output);
output.flush();
output.close();
/*********为了更快速的将图片上传,将缩小后的图片保存到暂存文件***************/
fileInput = new FileInputStream(filePath);
// 将图片内容解析成字节数组
mContent = getBytesFromInputStream(fileInput, 3500000);
fileInput.close();
/*********************/
// 预览一下你的图片吧
bm = bm.createScaledBitmap(bm, mButtomUpload.getWidth() - 10,
mButtomUpload.getHeight() - 10, true);
mButtomUpload.setImageBitmap(bm);
mButtomUpload.setPadding(2, 2, 2, 2);
} catch (Exception e) {
Utils.exceptionShow(CheckIn.this, e.getMessage());
}
}
附赠两个方法,一个将字节转换成bitmap,一个获取byte[]数组
private 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;
}
private static byte[] getBytesFromInputStream(InputStream is, int bufsiz)
throws IOException {
int total = 0;
byte[] bytes = new byte[4096];
ByteBuffer bb = ByteBuffer.allocate(bufsiz);
while (true) {
int read = is.read(bytes);
if (read == -1)
break;
bb.put(bytes, 0, read);
total += read;
}
byte[] content = new byte[total];
bb.flip();
bb.get(content, 0, total);
return content;
} // 拍照上传
private OnClickListener mUploadClickListener = new OnClickListener() {
public void onClick(View v) {
// 调用相机
Intent mIntent = new Intent("android.media.action.IMAGE_CAPTURE");
// 图片存储路径,可自定义
File tmpFile = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
// 获取这个图片的URI
originalUri = Uri.fromFile(tmpFile);//这是个实例变量,方便下面获取图片的时候用
mIntent.putExtra(MediaStore.EXTRA_OUTPUT, originalUri);
startActivityForResult(mIntent, ACTIVITY_IMAGE_CAPTURE);
}
};
// 打开相册
private OnClickListener mPicListClickListener = new OnClickListener() {
public void onClick(View v) {
// 调用相册
Intent mIntent= new Intent(Intent.ACTION_GET_CONTENT);
mIntent.addCategory(Intent.CATEGORY_OPENABLE);
mIntent.setType(MIME_TYPE_IMAGE_JPEG);
startActivityForResult(mIntent, ACTIVITY_GET_IMAGE);
}
};
监听事件写好了,怎么调用不用我说了吧。这是个startActivityForResult事件,对应的我们肯定得有个onActivityResult,贴之
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
Bitmap bm = null;
ContentResolver resolver = getContentResolver();
String filePath = "/sdcard/bengxin/bx_upload_tmp.jpg";//这个是暂存图片的路径
FileOutputStream output = null;
try {
// 创建暂存图片
if (Utils.CreateFile(filePath)) {
output = new FileOutputStream(filePath);
} else {
throw new Exception("内部错误");
}
if (requestCode == ACTIVITY_GET_IMAGE) {
// 获得图片的uri
originalUri = data.getData();
PS:拍照的那个URI我们在上面已经获取了
}
/**** 获取图片开始 ****/
//mContent是上传的图片byte[]数组,得到这个后随便怎么处理,当然你也可以直接用fileInput流
fileInput = (FileInputStream) resolver.openInputStream(Uri
.parse(originalUri.toString()));
// 将图片内容解析成字节数组
mContent = getBytesFromInputStream(fileInput, 3500000);
fileInput.close();
// 将字节数组转换为ImageView可调用的Bitmap对象
bm = getPicFromBytes(mContent, null);
/********* 获取图片完了 ************/
// 将图片缩小到指定比例并且保存到缓存文件
float scale = ((float) 210) / ((float) bm.getWidth());
bm = Utils.smallBmp(bm, scale);//这个缩小功能是自己写的方法
//将Bitmap读到文件中去,注意这个是压缩,那个100是压缩比,0-100,越大质量越好
bm.compress(CompressFormat.JPEG, 100, output);
output.flush();
output.close();
/*********为了更快速的将图片上传,将缩小后的图片保存到暂存文件***************/
fileInput = new FileInputStream(filePath);
// 将图片内容解析成字节数组
mContent = getBytesFromInputStream(fileInput, 3500000);
fileInput.close();
/*********************/
// 预览一下你的图片吧
bm = bm.createScaledBitmap(bm, mButtomUpload.getWidth() - 10,
mButtomUpload.getHeight() - 10, true);
mButtomUpload.setImageBitmap(bm);
mButtomUpload.setPadding(2, 2, 2, 2);
} catch (Exception e) {
Utils.exceptionShow(CheckIn.this, e.getMessage());
}
}
附赠两个方法,一个将字节转换成bitmap,一个获取byte[]数组
private 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;
}
private static byte[] getBytesFromInputStream(InputStream is, int bufsiz)
throws IOException {
int total = 0;
byte[] bytes = new byte[4096];
ByteBuffer bb = ByteBuffer.allocate(bufsiz);
while (true) {
int read = is.read(bytes);
if (read == -1)
break;
bb.put(bytes, 0, read);
total += read;
}
byte[] content = new byte[total];
bb.flip();
bb.get(content, 0, total);
return content;