照片
1.跳转到拍照界面
File imageFile = new File(Environment
.getExternalStorageDirectory(), "tempImage.jpg");
// 如果存在就删了重新创建
try {
if (imageFile.exists()) {
imageFile.delete();
}
imageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
// 将存储地址转化成uri对象
imageUri =Uri.fromFile(imageFile);
// 设置打开照相的Intent
Intent intent4 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 设置相片的输出uri为刚才转化的imageUri
intent4.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// 开启相机程序,设置requestCode为TOKE_PHOTO
startActivityForResult(intent4, IMAGE_MY);
2.拍照界面返回后,跳转到裁剪界面
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_MY && resultCode == RESULT_OK){
// 设置intent为启动裁剪程序
Intent intent = new Intent("com.android.camera.action.CROP");
// 设置Data为刚才的imageUri和Type为图片类型
intent.setDataAndType(imageUri, "image/*");
// 设置可缩放
intent.putExtra("scale", true);
// 设置输出地址为imageUri
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// 开启裁剪,设置requestCode为CROP_PHOTO
startActivityForResult(intent, CROP_PHOTO);
}
if (requestCode == CROP_PHOTO){
Bitmap bitmap;
try {
//通过BitmapFactory将imageUri转化成bitmap
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
//设置显示
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
选择图片
1.转到照片选择界面
Intent intent4 = new Intent(Intent.ACTION_GET_CONTENT);
intent4.setType("image/*");
startActivityForResult(intent4,IMAGE_MY);
2.选择照片界面返回
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//从照片选择界面返回
if (requestCode == IMAGE_MY && resultCode == RESULT_OK){
Uri uri = data.getData();
//将uri转化为对应的本地路径
String path = getFilePath(MainActivity.this,uri);
Bitmap bitmap = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(bitmap);
}
}
3.将uri转换为对应的路径
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getFilePath(Context context, Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider 是否外部存储内容提供者
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0]; if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
} // TODO handle non-primary volumes
}
// DownloadsProvider 是否下载内容提供者
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider 是否多媒体内容提供者
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
private static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* 判断外部存储是否可用
*
*
*/
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
Log.e("", "ExternalStorage not mounted");
return false;
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}