package com.testCarema.android;
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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class testCarema 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.imageView);
imgViewListener = new OnClickListener()
{
public void onClick ( View v )
{
final CharSequence[] items =
{ "相册", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(testCarema.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, 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();
}
};
// 给imageView控件绑定点点击监听器
imageView.setOnClickListener(imgViewListener);
}
@ Override
protected void onActivityResult ( int requestCode , int resultCode , Intent data )
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
ContentResolver resolver = getContentResolver();
/**
* 因为两种方式都用到了startActivityForResult方法,
* 这个方法执行完后都会执行onActivityResult方法, 所以为了区别到底选择了那个方式获取图片要进行判断,
* 这里的requestCode跟startActivityForResult里面第二个参数对应
*/
if (requestCode == 0)
{
try
{
// 获得图片的uri
Uri originalUri = data.getData();
// 将图片内容解析成字节数组
mContent = readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
// 将字节数组转换为ImageView可调用的Bitmap对象
myBitmap = getPicFromBytes(mContent, null);
// //把得到的图片绑定在控件上显示
imageView.setImageBitmap(myBitmap);
} catch ( Exception e )
{
System.out.println(e.getMessage());
}
} else if (requestCode == 1)
{
try
{
super.onActivityResult(requestCode, resultCode, data);
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 )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// 把得到的图片绑定在控件上显示
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 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;
}
}
yxkfw
- 粉丝: 82
- 资源: 2万+
最新资源
- SQL语言详细教程:从基础到高级全面解析及实际应用
- 仓库管理系统源代码全套技术资料.zip
- 计算机二级考试详细试题整理及备考建议
- 全国大学生电子设计竞赛(电赛)历年试题及备考指南
- zigbee CC2530网关+4节点无线通讯实现温湿度、光敏、LED、继电器等传感节点数据的采集上传,网关通过ESP8266上传远程服务器及下发控制.zip
- 云餐厅APP项目源代码全套技术资料.zip
- vscode 翻译插件开发,选中要翻译的单词,使用快捷键Ctrl+Shift+T查看翻译
- mrdoc-alpine0.9.2
- ACMNOICSP比赛经验分享:从知识储备到团队协作的全面指南
- 云餐厅项目源代码全套技术资料.zip
- 基于STM32的数字闹钟系统的仿真和程序
- 混合信号设计中DEF文件创建流程
- 美国大学生数学建模竞赛(美赛)详细教程:从组队到赛后总结全攻略
- 病媒生物孳生地调查和治理工作方案.docx
- 保姆的工作标准.docx
- 病媒生物防制指南.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈