android调动系统的照相机并把照片显示在ImageView上

L-D-Y 2012-10-10 11:17:06
我知道调用系统照相机是
Intent intent = new Intent();
intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
startActivity(intent);
怎么把拍过的照片显示到ImageView上面呢?求大神指点谢谢了
...全文
3000 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
BadPattern 2012-10-11
  • 打赏
  • 举报
回复
思路:
使用ContentResolver访问系统提供的MediaProvider数据库,得到照片的路径,使用BitmapFactory的decodeXXX系列方法decode出来,当然不能直接将图片直接放进内存中,decode之前需要做一些计算,缩放到分辨率及内存占用都可行的情况下转换为Bitmap显示到ImageView里面。
L-D-Y 2012-10-11
  • 打赏
  • 举报
回复
谢谢了 很有帮助 我先看着
fire_fire_fire 2012-10-11
  • 打赏
  • 举报
回复
你调用了相机或从相册选取相片,返回的时候会调用onActivityResult这个方法,处理相片等方法就在这个里面
fire_fire_fire 2012-10-11
  • 打赏
  • 举报
回复
你看看下面的方法对你有没有用,我从自己的项目中拷出来的,有些变量没有初始化是因为它是全局的,初始化不在这个方法里面,你自己看着来

/**
* 从照片文件夹中选择完图片后会调用这个方法
*/
@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;
}
}
}
L-D-Y 2012-10-10
  • 打赏
  • 举报
回复
String strPath = Environment.getExternalStorageDirectory().toString();
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);
这样就存起来了 可是在照相的时候怎么看呢?就是预览
L-D-Y 2012-10-10
  • 打赏
  • 举报
回复
这不许要存储吗?
liangchichexin 2012-10-10
  • 打赏
  • 举报
回复
用startActivityForResult而不是startActivity
[Quote=引用 1 楼 的回复:]

你应该用onActivityResult,像这样
Java code
public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;

@Override
public void onCreate(Bun……
[/Quote]
liangchichexin 2012-10-10
  • 打赏
  • 举报
回复
你应该用onActivityResult,像这样
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);
}
}

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧