从谷歌官方文档的描述来看, 在 Android N版本 以上,直接将file://形式的Uri暴露给了APP应用,会抛出 FileUriExposedException 异常。
Android N以下版本,仍然可以使用 file:// 形式的Uri,但强烈不推荐。
不推荐使用 file:// 的原因在于:
Android N以上版本,将使用 content:// 代替 file:// 形式的Uri,系统将赋予APP临时的权限去获取目标Uri的资源。
FileProvider 是ContentProvider的一个子类。使用 FileProvider可以创建 content:// 形式的Uri,保证APP对文件资源的安全共享。
要使用FileProvider解决Android N以上版本androidosFileUriExposedException的问题,首先要在AndroidManifestxml注册该FileProvider:
然后在res文件夹里添加创建xml/file_pathsxml文件:
创建 content:// Uri,通过FileProvidergetUriForFile(Context context, String authorities, File file)实现。为了实现兼容,可通过根据设备系统使用不同的方法获取Uri:
这样就同时兼容 file:// 和 content:// 的Uri形式,下面的例子是调用系统相机拍照时,通过FileUtils获取照片保存路径的Uri:
参考博客:
1、实现原理:用户打开相册或相机选择相片后,相片经过压缩并设置在控件上,在本地sd卡存一份(如果有的话,没有则内部存储,所以还需要判断用户是否挂载了sd卡),然后在服务器上存储一份该,当下次再次启动应用时,会默认去sd卡加载该,如果本地没有,再会去联网请求
2、使用了picasso框架以及自定义BitmapUtils工具类
3、记得加上相关权限
<uses-permission android:name="androidpermissionINTERNET"></uses-permission>
<uses-permission android:name="androidpermissionCAMERA"/>
<uses-permission android:name="androidpermissionWRITE_EXTERNAL_STORAGE"></uses-permission>
/
public class MainActivity extends AppCompatActivity implements ViewOnClickListener {
private ImageView iv;//要设置的头像
private Button btn_photo;//调用相册按钮
private Button btn_camera;//调用相机按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutactivity_main);
iv=(ImageView) findViewById(Ridiv);
btn_photo = (Button) findViewById(Ridbtn_photo);
btn_camera = (Button) findViewById(Ridbtn_camera);
btn_photosetOnClickListener(this);
btn_camerasetOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (vgetId()) {
case Ridbtn_photo://打开系统相册
Intent intent=new Intent(IntentACTION_PICK, MediaStoreImagesMediaEXTERNAL_CONTENT_URI);
startActivityForResult(intent,100);
break;
case Ridbtn_camera://打开系统相机
Intent intent2=new Intent(MediaStoreACTION_IMAGE_CAPTURE);
startActivityForResult(intent2,200);
break;
}
}
@RequiresApi(api = BuildVERSION_CODESKITKAT)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
superonActivityResult(requestCode, resultCode, data);
if(requestCode==100&&resultCode==RESULT_OK&&data!=null){//系统相册
Uri imageData = datagetData();
String path=getPath(imageData);
Bitmap bitmap = BitmapFactorydecodeFile(path);
Bitmap bitmap1 = BitmapUtilszoom(bitmap, ivgetWidth(), ivgetHeight());
Bitmap bitmap2 = BitmapUtilscircleBitmap(bitmap1);
//加载显示
ivsetImageBitmap(bitmap2);
//bitmap上传到服务器
//bitmap保存到本地
saveImage(bitmap2);
}else if(requestCode==200&&resultCode==RESULT_OK&&data!=null){//系统相机
Bitmap bitmap = (Bitmap) datagetExtras()get("data");
BitmapUtilszoom(bitmap,ivgetWidth(),ivgetHeight());
bitmap=BitmapUtilscircleBitmap(bitmap);
//加载显示
ivsetImageBitmap(bitmap);
//bitmap上传到服务器
//bitmap保存到本地
saveImage(bitmap);
}
}
/
数据的存储。(5种)
Bimap:内存层面的对象。
存储--->内存:
BitmapFactorydecodeFile(String filePath);
BitmapFactorydecodeStream(InputStream is);
内存--->存储:
bitmapcompress(BitmapCompressFormatPNG,100,OutputStream os);
/
private void saveImage(Bitmap bitmap) {
File filesDir;
if(EnvironmentgetExternalStorageState()equals(EnvironmentMEDIA_MOUNTED)){//判断sd卡是否挂载
//路径1:storage/sdcard/Android/data/包名/files
filesDir = thisgetExternalFilesDir("");
}else{//手机内部存储
//路径:data/data/包名/files
filesDir = thisgetFilesDir();
}
FileOutputStream fos = null;
try {
File file = new File(filesDir,"iconpng");
fos = new FileOutputStream(file);
bitmapcompress(BitmapCompressFormatPNG, 100,fos);
} catch (FileNotFoundException e) {
eprintStackTrace();
}finally{
if(fos != null){
try {
fosclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
//如果本地有,就不需要再去联网去请求
private boolean readImage() {
File filesDir;
if(EnvironmentgetExternalStorageState()equals(EnvironmentMEDIA_MOUNTED)){//判断sd卡是否挂载
//路径1:storage/sdcard/Android/data/包名/files
filesDir = getExternalFilesDir("");
}else{//手机内部存储
//路径:data/data/包名/files
filesDir = getFilesDir();
}
File file = new File(filesDir,"iconpng");
if(fileexists()){
//存储--->内存
Bitmap bitmap = BitmapFactorydecodeFile(filegetAbsolutePath());
ivsetImageBitmap(bitmap);
return true;
}
return false;
}
@RequiresApi(api = BuildVERSION_CODESKITKAT)
private String getPath(Uri uri) {
int sdkVersion = BuildVERSIONSDK_INT;
//高于442的版本
if (sdkVersion >= 19) {
Loge("TAG", "uri auth: " + urigetAuthority());
if (isExternalStorageDocument(uri)) {
String docId = DocumentsContractgetDocumentId(uri);
String[] split = docIdsplit(":");
String type = split[0];
if ("primary"equalsIgnoreCase(type)) {
return EnvironmentgetExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContractgetDocumentId(uri);
final Uri contentUri = ContentUriswithAppendedId(Uriparse("content://downloads/public_downloads"),
LongvalueOf(id));
return getDataColumn(this, contentUri, null, null);
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContractgetDocumentId(uri);
final String[] split = docIdsplit(":");
final String type = split[0];
Uri contentUri = null;
if ("image"equals(type)) {
contentUri = MediaStoreImagesMediaEXTERNAL_CONTENT_URI;
} else if ("video"equals(type)) {
contentUri = MediaStoreVideoMediaEXTERNAL_CONTENT_URI;
} else if ("audio"equals(type)) {
contentUri = MediaStoreAudioMediaEXTERNAL_CONTENT_URI;
}
final String selection = "_id=";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(this, contentUri, selection, selectionArgs);
} else if (isMedia(uri)) {
String[] proj = {MediaStoreImagesMediaDATA};
Cursor actualimagecursor = thismanagedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursorgetColumnIndexOrThrow(MediaStoreImagesMediaDATA);
actualimagecursormoveToFirst();
return actualimagecursorgetString(actual_image_column_index);
}
} else if ("content"equalsIgnoreCase(urigetScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return urigetLastPathSegment();
return getDataColumn(this, uri, null, null);
}
// File
else if ("file"equalsIgnoreCase(urigetScheme())) {
return urigetPath();
}
return null;
}
/
uri路径查询字段
@param context
@param uri
@param selection
@param selectionArgs
@return
/
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 = contextgetContentResolver()query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursormoveToFirst()) {
final int index = cursorgetColumnIndexOrThrow(column);
return cursorgetString(index);
}
} finally {
if (cursor != null)
cursorclose();
}
return null;
}
private boolean isExternalStorageDocument(Uri uri) {
return "comandroidexternalstoragedocuments"equals(urigetAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "comandroidprovidersdownloadsdocuments"equals(urigetAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "comandroidprovidersmediadocuments"equals(urigetAuthority());
}
public static boolean isMedia(Uri uri) {
return "media"equals(urigetAuthority());
}
/
@param uri The Uri to check
@return Whether the Uri authority is Google Photos
/
public static boolean isGooglePhotosUri(Uri uri) {
return "comgoogleandroidappsphotoscontent"equals(urigetAuthority());
}
/
判断本地是否有该,没有则去联网请求
/
@Override
protected void onResume() {
superonResume();
if(readImage()){
return;
}
}
}
//BitmapUtils工具类public class BitmapUtils { /
该方法用于将进行圆形处理
/ public static Bitmap circleBitmap(Bitmap source){ //默认只对宽进行处理 int width=sourcegetWidth(); Bitmap bitmap=BitmapcreateBitmap(width,width,BitmapConfigARGB_8888); Canvas canvas=new Canvas(bitmap); Paint paint=new Paint(); //设置抗锯齿 paintsetAntiAlias(true); canvasdrawCircle(width/2,width/2,width/2,paint); paintsetXfermode(new PorterDuffXfermode(PorterDuffModeSRC_IN)); canvasdrawBitmap(source,0,0,paint); return bitmap; } / 该方法用于压缩处理,注意width、height参数的类型必须是float / public static Bitmap zoom(Bitmap source,float width,float height){ Matrix matrix=new Matrix(); //进行压缩处理 matrixpostScale(width/sourcegetWidth(),height/sourcegetHeight()); Bitmap bitmap = BitmapcreateBitmap(source, 0, 0, sourcegetWidth(), sourcegetHeight(), matrix, false); return bitmap; }}
以上所述是小编给大家介绍的Android实现调用系统图库与相机设置头像并保存在本地及服务器 ,希望对大家有所帮助
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)