在onActivityResult方法里面
/**
这个是调用相册的path
*/
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null,null, null);cursor.moveToFirst();
String imageFilePath = cursor.getString(1);
FileInputStream fis = new FileInputStream(imageFilePath);
bitmap = BitmapFactory.decodeStream(fis);
fis.close();
cursor.close();
/**
调用相机
*/
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String name = formatter.format(System.currentTimeMillis()) + ".jpg";
Log.i("zhiwei.zhao", "image name:" + name);
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
Bundle bundle = data.getExtras();
/* 获取相机返回的数据,并转换为Bitmap图片格式 */
Bitmap bitmap = (Bitmap) bundle.get("data");
FileOutputStream b = null;
String path = Environment.getExternalStorageDirectory().getPath();
File file = new File(path + "/myImage/");
/** 检测文件夹是否存在,不存在则创建文件夹 **/
if (!file.exists() && !file.isDirectory())
file.mkdirs();
String fileName = file.getPath() + "/" + name;
Log.i("zhiwei.zhao", "camera file path:" + fileName);
try {
b = new FileOutputStream(fileName);
/* 把数据写入文件 */
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (b == null)
return;
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 调用相机拍照
*/
private void cameraPhoto() {
String sdStatus = Environment.getExternalStorageState();
/* 检测sd是否可用 */
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "SD卡不可用!", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, CAMERA_INTENT_REQUEST);
}
/**
* 打开系统相册
*/
private void systemPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SYS_INTENT_REQUEST);
}
权限:
<!-- 往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<!-- 在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>