首先判断版本,判断是否授权,如果未授权,采用弹出框动态授权。
如果版本号大于7.0采用虚拟路径临时保存,如果版本号小于7.0采用绝对路径。
先使用PackageManager.checkPermission检测对方的app有没有取得文件读写权限。如果有的话,给对方发送file://格式URI。如果没有的话,给对方发送FileProvider生成的URI并临时授权
1.easypermissions动态权限申请框架
implementation 'pub.devrel:easypermissions:3.0.0'
2.相关权限AndroidMainfast.xml
FileProvider的使用(Android 7.0 行为变更 通过FileProvider在应用间共享文件)
第一步 声明FIleProvider
为什么要声明呢?
因为FileProvider是ContentProvider子类
注意需要设置一个meta-data,里面指向一个xml文件。
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
······
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.baoming.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
3.xml文件provider_paths.xml
为什么要写这么个xml文件?
因为要使用content://uri替代file://uri,那么,content://的uri如何定义呢?总不能使用文件路径。
所以,需要一个虚拟的路径对文件路径进行映射,所以需要编写个xml文件,通过path以及xml节点确定可访问的目录,通过name属性来映射真实的文件路径。
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="camera_photos" path="."/>
</paths>
4.MainActivity.java,布局类代码省略,2个按钮一个imageview控件
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.util.List;
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{
private static final int RC_EXTERNAL_STORAGE_PERM = 100;
private String bitmappath;
private Uri cramuri;
private File file2;
private File currentImageFile = null;
private Context context;
private ImageView imageView;
private Button btcamera;
private Button btablum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
imageView=findViewById(R.id.imageView);
btablum=findViewById(R.id.btablum);
btcamera=findViewById(R.id.btcamera);
requiresPermission();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
}
btcamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
currentImageFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg");
}
// 启动系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
cramuri = FileProvider.getUriForFile(MainActivity.this, "com.fileprovider", currentImageFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
cramuri = Uri.fromFile(currentImageFile);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, cramuri);
startActivityForResult(intent,2);
}
});
btablum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent,1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
// 调用相机后返回
case 1:
if (resultCode == RESULT_OK) {
if (data != null) {
// 得到图片的全路径
Uri uri = data.getData();
startPhotoZoom(uri);
}
}
break;
case 2:
if (resultCode == RESULT_OK) {
startPhotoZoom(cramuri);
}
break;
case 3:
imageView.setImageURI(Uri.fromFile(file2));
String file = ImageUtils.getRealPathFromUri(MainActivity.this, Uri.fromFile(file2));
//转base64
bitmappath = ImageUtils.getImgStr(file);
}
}
//裁剪并压缩图片
public void startPhotoZoom(Uri uri) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
file2 = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg");
}
Intent intent = new Intent("com.android.camera.action.CROP"); // 裁剪图片意图
intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);//裁剪框 X 比值
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 600);//裁剪后输出宽度
intent.putExtra("outputY", 600);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file2));
intent.putExtra("return-data", false); //是否在Intent中返回数据
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 图片格式
intent.putExtra("noFaceDetection", true);// 取消人脸识别
startActivityForResult(intent, 3);
}
@AfterPermissionGranted(RC_EXTERNAL_STORAGE_PERM)
private void requiresPermission() {
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
if (EasyPermissions.hasPermissions(MainActivity.this, perms)) {
} else {
EasyPermissions.requestPermissions(this, getResources().getString(R.string.tips_crema), RC_EXTERNAL_STORAGE_PERM, perms);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, @NonNull List perms) {
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List perms) {
new AppSettingsDialog.Builder(this)
.setTitle(getResources().getString(R.string.tips_limit))
.setPositiveButton(getResources().getString(R.string.tips_limit_true))
.setNegativeButton(getResources().getString(R.string.tips_limit_false))
.setRationale(getResources().getString(R.string.tips_limit_crema))
.setRequestCode(RC_EXTERNAL_STORAGE_PERM)
.build()
.show();
}
}
4.ImageUtils.java工具类
public class ImageUtils {
/**
* 根据图片的Uri获取图片的绝对路径。@uri 图片的uri
* @return 如果Uri对应的图片存在,那么返回该图片的绝对路径,否则返回null
*/
public static String getRealPathFromUri(Context context, Uri uri) {
if(context == null || uri == null) {
return null;
}
if("file".equalsIgnoreCase(uri.getScheme())) {
return getRealPathFromUri_Byfile(context,uri);
} else if("content".equalsIgnoreCase(uri.getScheme())) {
return getRealPathFromUri_Api11To18(context,uri);
}
return getRealPathFromUri_AboveApi19(context, uri);//没用到
}
//针对图片URI格式为Uri:: file:///storage/emulated/0/DCIM/Camera/IMG_20170613_132837.jpg
private static String getRealPathFromUri_Byfile(Context context,Uri uri){
String uri2Str = uri.toString();
String filePath = uri2Str.substring(uri2Str.indexOf(":") + 3);
return filePath;
}
/**
* 适配api19以上,根据uri获取图片的绝对路径
*/
@SuppressLint("NewApi")
private static String getRealPathFromUri_AboveApi19(Context context, Uri uri) {
String filePath = null;
String wholeID = null;
wholeID = DocumentsContract.getDocumentId(uri);
// 使用':'分割
String id = wholeID.split(":")[1];
String[] projection = { MediaStore.Images.Media.DATA };
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = { id };
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
selection, selectionArgs, null);
int columnIndex = cursor.getColumnIndex(projection[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
/**
* //适配api11-api18,根据uri获取图片的绝对路径。
* 针对图片URI格式为Uri:: content://media/external/images/media/1028
*/
private static String getRealPathFromUri_Api11To18(Context context, Uri uri) {
String filePath = null;
String[] projection = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(context, uri, projection, null,
null, null);
Cursor cursor = loader.loadInBackground();
if (cursor != null) {
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
}
return filePath;
}
/**
* 适配api11以下(不包括api11),根据uri获取图片的绝对路径
*/
private static String getRealPathFromUri_BelowApi11(Context context, Uri uri) {
String filePath = null;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(uri, projection,
null, null, null);
if (cursor != null) {
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
}
return filePath;
}
/**
* 将图片转换成Base64编码
* @param path 待处理图片
* @return
*/
public static String getImgStr(String path) {
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
//imgFile = "D:\\testPhoto.jpg";
if(TextUtils.isEmpty(path)){
return null;
}
InputStream is = null;
byte[] data = null;
String result = null;
try{
is = new FileInputStream(path);
//创建一个字符流大小的数组。
data = new byte[is.available()];
//写入数组
is.read(data);
//用默认的编码格式进行编码
result = Base64.encodeToString(data,Base64.DEFAULT);
}catch (Exception e){
e.printStackTrace();
}finally {
if(null !=is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
<string name="tips_crema">当前App需要申请读写文件和调用相机权限才能运行</string>
<string name="tips_limit">权限申请</string>
<string name="tips_limit_true">确认</string>
<string name="tips_limit_false">取消</string>
<string name="tips_limit_crema">当前App需要申请读写文件和调用相机权限,需要打开设置页面么?</string>