赞
踩
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.
Bitmap bitmap = Util.makeBitmap(data, 50 * 1024);
bitmap = Util.rotate(bitmap, degree);
return bitmap;
}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");
finish();
}
} else {
// Save the image to a temp file and invoke the cropper
Uri tempUri = null;
FileOutputStream tempStream = null;
try {
File path = getFileStreamPath(sTempCropFilename);
path.delete();
tempStream = openFileOutput(sTempCropFilename, 0);
tempStream.write(data);
tempStream.close();
tempUri = Uri.fromFile(path);
} catch (FileNotFoundException ex) {
setResult(Activity.RESULT_CANCELED);
finish();
return;
} catch (IOException ex) {
setResult(Activity.RESULT_CANCELED);
finish();
return;
} finally {
Util.closeSilently(tempStream);
}
Bundle newExtras = new Bundle();
if (mCropValue.equals("circle")) {
newExtras.putString("circleCrop", "true");
}
if (mSaveUri != null) {
newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
} else {
newExtras.putBoolean("return-data", true);
}
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setData(tempUri);
cropIntent.putExtras(newExtras);
startActivityForResult(cropIntent, CROP_MSG);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。