1.在调用android系统相册时,使用的是如下方式:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/");
startActivityForResult(intent,1);
在onActivityResult 中通过data获取数据可以正常获取:
if (requestCode == 1)
{
Uri uri = null;
if (null != data || resultCode == RESULT_OK)
{
uri = data.getData();
String filePath = getRealPathFromUri(this,uri);
Log.d("filepath",filePath);
if (null == filePath || filePath.equals(""))
{
return;
}
// To do something
//得到了文件路径,可以执行相应压缩上传操作,
}
}
super.onActivityResult(requestCode, resultCode, data);
public static String getRealPathFromUri(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
if (null == cursor )
{
return "";
}
if (cursor.moveToFirst())
{
return cursor.getString(0);
}
}catch (Exception e)
{
Log.w("resolvepic",e.getMessage());
}
finally {
if (cursor != null) {
cursor.close();
}
}
return "";
}
2.回到正题,如何调用系统照相机呢?如下:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定存放拍摄照片的位置
File filePath = createImageFile();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(filePath));
startActivityForResult(intent, 2);
------------------------------------------------------我是分隔线------------------------------------------------------
private File createImageFile() throws IOException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
String timeStamp = format.format(System.currentTimeMillis());
String imageFileName = "hxjysj_" + timeStamp + ".jpg";
File image = new File(PictureUtil.getAlbumDir(this), imageFileName);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
在onActivityResult中:
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
其中intent得到的结果总是为null???
查看相关资料发现,如果指定intent.putExtra(MediaStore.EXTRA_OUT,uri);
则intent拿到的是null. 如果没有指定uri,则intent就会有数据的返回.
照相机会有自己的默认的存储路径,如果想得到图片的路径,则可以通过
Bitmap bitmap = (Bitmap)intent.getParcelableExtra("data");
得到bitmap再进行图片的压缩展示等相关的操作.
3.附Camera.java部分源码:
private Bitmap createCaptureBitmap(byte[] data) { // This is really stupid...we just want to read the orientation in // the jpeg header. String filepath = ImageManager.getTempJpegPath(); int degree = 0; if (saveDataToFile(filepath, data)) { degree = ImageManager.getExifOrientation(filepath); new File(filepath).delete(); }
//缩放的位图bitmap只有50k,如果还想得到高质量的图片还是指定图片路径吧.// Limit to 50k pixels so we can return it in the intent.
private void doAttach() { if (mPausing) { return; } byte[] data = mImageCapture.getLastCaptureData(); if (mCropValue == null) { // First handle the no crop case -- just return the value. If the // caller specifies a "save uri" then write the data to it's // stream. Otherwise, pass back a scaled down version of the bitmap // directly in the extras.
//如果指定uri就会保存到指定的uri中,否则,通过缩放位图的缩放版本
if (mSaveUri != null) {
//这里说明的就是设置了指定保存的uri地址
OutputStream outputStream = null;
try {
outputStream = mContentResolver.openOutputStream(mSaveUri);
outputStream.write(data);
outputStream.close();
//设置了成功的返回状态.
setResult(RESULT_OK);
finish();
} catch (IOException ex) {
// ignore exception
} finally {
Util.closeSilently(outputStream);
}
} else {
Bitmap bitmap = createCaptureBitmap(data);
setResult(RESULT_OK,
new Intent("inline-data").putExtra("data", bitmap));
//如果没有设置指定的保存地址.intent就会有数据返回.
//在onActivityResult中可以通过如下方式得到一个bitmap对象再进行操作:
//Bitmap bitmap = (Bitmap)intent.getParcelableExtra("data");