@Tyhj
2017-04-21T20:24:38.000000Z
字数 7577
阅读 2563
Android
原文:https://www.zybuluo.com/Tyhj/note/730704
之前已经写过用摄像头和相册获取图片了,每一次都觉得是正解,每一次后来都发现自己很菜。最近发现项目在Android 7.0时候发生了一些权限问题。主要是Uri的获取,还有访问文件夹出了问题。
String path;//保存图片,
public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2;
public static final int PICK_PHOTO = 0;
String date;//根据时间来获取的图片的名字
//随机获取文件名字
public void getDate() {
date = System.currentTimeMillis() + ".JPEG";
}
path = getExternalCacheDir() + "/Jubaoji";//写在onCreate中,用这个路径可以避过访问文件夹的权限
引用:
我先科普一个概念叫做应用关联缓存目录:就是指SD卡中专门用于存放当前应用缓存数据的位置,调用getExternalCacheDir()可以得到这个目录。具体路径是:/sdcard/Android/data//cache
那么为什么要使用应用关联缓存目录来存放图片呢?因为从android6.0开始读写SD卡被列为了危险权限,如果将图片放在SD卡的其他地方,都要进行运行时权限处理才行,而使用应用缓存数据的位置则可以跳过这一步
打开相机获取图象,并保存到file这个文件,path和date就不会改变了
getDate();
File file = new File(path, date);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0及以上
Uri uriForFile = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
}
startActivityForResult(intent, TAKE_PHOTO);
获取到相机的数据后,把file转成Uri调用系统工具剪裁图片
//这是从相机返回的数据
case TAKE_PHOTO:
if (resultCode == PersonalSetting.RESULT_OK) {
File file = new File(path, date);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri inputUri = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", file);//通过FileProvider创建一个content类型的Uri
cropPhoto(inputUri);
} else {
cropPhoto(Uri.fromFile(file));
}
}
break;
剪裁图片,
//剪裁图片
public void cropPhoto(Uri imageUri) {
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
File file = new File(path, date);
Uri outPutUri = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", file);
intent.setDataAndType(imageUri, "image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
intent.putExtra("noFaceDetection", false);//去除默认的人脸识别,否则和剪裁匡重叠
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
intent.setDataAndType(imageUri, "image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
}
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
startActivityForResult(intent, CROP_PHOTO);
}
//剪裁后返回的图片
//剪裁图片返回数据,就是原来的文件
case CROP_PHOTO:
if (resultCode == PersonalSetting.RESULT_OK) {
String fileName = path + "/" + date;
File newFile = new File(path, date);
//我在这里压缩了一下图片
Defined.ImgCompress(fileName, newFile, 500, 500, 300);
//获取到的就是new File或fileName
}
打开相册选取图片
getDate();
dialog.cancel();
Intent intent = new Intent(ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra("crop", true);
intent.putExtra("scale", true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果大于等于7.0使用FileProvider
Uri uriForFile = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", new File(path,date));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
}
startActivityForResult(intent, PICK_PHOTO);
//获取从相册返回的图片,为了避免破坏原来的图片,复制一份,再获取Uri,然后拿去剪裁
//这是从相册返回的数据
case PICK_PHOTO:
if (resultCode == PersonalSetting.RESULT_OK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果大于等于7.0使用FileProvider
File path_pre = new File(GetImagePath.getPath(PersonalSetting.this, data.getData()));
File newFile = new File(path, date);
Toast.makeText(PersonalSetting.this, getString(R.string.msg_loading), Toast.LENGTH_SHORT).show();
try {
Defined.copyFile(path_pre, newFile);
Uri dataUri = FileProvider.getUriForFile(PersonalSetting.this, "demand.example.tyhj.jubao.fileProvider", newFile);
cropPhoto(dataUri);
} catch (IOException e) {
e.printStackTrace();
}
} else {
String path_pre = GetImagePath.getPath(PersonalSetting.this, data.getData());
File newFile = new File(path, date);
Toast.makeText(PersonalSetting.this, getString(R.string.msg_loading), Toast.LENGTH_SHORT).show();
try {
Defined.copyFile(new File(path_pre), newFile);
} catch (IOException e) {
e.printStackTrace();
}
cropPhoto(Uri.fromFile(newFile));
}
}
break;
之后和相机返回的是一样的,然后通过Uri获取文件路径方法也不是很简单,如下应该是比较好的,国外大神的方法
package tools;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
public class GetImagePath {
// 4.4以上 content://com.android.providers.media.documents/document/image:3952
// 4.4以下 content://media/external/images/media/3951
@SuppressLint("NewApi")
public static String getPath(final Context context, final 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];
}
}
// 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 the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
//Android 4.4以下版本自动使用该方法
public 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;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
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());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}