我知道调用系统照相机是
Intent intent = new Intent();
intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
startActivity(intent);
怎么把拍过的照片显示到ImageView上面呢?求大神指点谢谢了
Intent intent = new Intent();
intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
startActivity(intent);
怎么把拍过的照片显示到ImageView上面呢?求大神指点谢谢了
解决方案 »
- 有谁在android中用过scriptengineManager这个API没有?
- 关于获取已安装应用程序图标
- 帮忙看下这个*.this其什么作用啊,可不可以写成this.*
- SurfaceHolder.lockCanvas() = null的问题
- android手机端怎么直接访问服务器
- 请问一个在列表控件中有一个倒三角形的那个是什么控件
- android 模拟器和真机的性能差别
- SD卡文件夹下,有几千个录音文件,删除极慢,怎么解决?
- 请问下,如何能让某应用在设置里面卸载的时候, 能监控到,并不允许卸载
- android apk如何用OpenSSL进行签名与验证
- 应用怎样拥有/data/local/tmp目录的写权限
- android的下按返回键异常
public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
File path = new File(strPath);
if(!path.exists())
path.mkdirs();
strFileName = "test.jpg";
strFilePath = strPath + "/" + strFileName; File file = new File(strFilePath);
Uri uri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
这样就存起来了 可是在照相的时候怎么看呢?就是预览
* 从照片文件夹中选择完图片后会调用这个方法
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PHOTO_PICKED_WITH_DATA: {// 调用(相册)Gallery返回的
try {
// 获得图片的uri
originalUri = data.getData();
} catch (Exception e) {
System.out.println(e.getMessage());
}
// 获取路径---------------------------
String[] proj = { MediaStore.Images.Media.DATA };
// 好像是android多媒体数据库的封装接口,具体的看Android文档
Cursor cursor = managedQuery(originalUri, proj, null, null, null);
// 按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
// 将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
// 最后根据索引值获取图片路径
userSelectPath = cursor.getString(column_index);
if (userSelectPath.indexOf("/") != -1) {
imgName = userSelectPath.substring(userSelectPath
.lastIndexOf("/") + 1);
}
try {
BitmapFactory.Options options = new BitmapFactory.Options();
// 先设置为TRUE不加载到内存中,但可以得到宽和高
options.inJustDecodeBounds = true;
bm = BitmapFactory.decodeFile(userSelectPath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / (float) 200);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 这样就不会内存溢出了
Bitmap bm = BitmapFactory.decodeFile(userSelectPath, options);
Handler h=new Handler();
h.post(new Runnable() {
@Override
public void run() {
rl_send_pic.setVisibility(View.VISIBLE);
img_send_pic.setImageBitmap(bm);
isImg = true;
// bm = null;
}
});
} catch (Exception e) {
}
break;
}
// 照相机程序返回的,再次调用图片剪辑程序去修剪图片
case CAMERA_WITH_DATA: {
try {
userSelectPath = PHOTO_DIR + "/" + picName;
imgName = picName;
BitmapFactory.Options options = new BitmapFactory.Options();
// 先设置为TRUE不加载到内存中,但可以得到宽和高
options.inJustDecodeBounds = true;
bm = BitmapFactory.decodeFile(userSelectPath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / (float) 200);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 这样就不会内存溢出了
bm = BitmapFactory.decodeFile(userSelectPath, options);
h.post(new Runnable() {
@Override
public void run() {
rl_send_pic.setVisibility(View.VISIBLE);
img_send_pic.setImageBitmap(bm);//放预览图的控件
isImg = true;
}
});
} catch (Exception e) {
System.out.println("e=" + e);
}
break;
}
}
}
使用ContentResolver访问系统提供的MediaProvider数据库,得到照片的路径,使用BitmapFactory的decodeXXX系列方法decode出来,当然不能直接将图片直接放进内存中,decode之前需要做一些计算,缩放到分辨率及内存占用都可行的情况下转换为Bitmap显示到ImageView里面。