由于Android7.0对权限的一些改动,我们不分手机无法访问到文件地址
1.AndroidManifest.xml 增加provider定义
<provider
android:name="android.support.v4.content.FileProvider" //固定
android:authorities="com.lele.avatarcircledemo.fileprovider"//路径 前面为包名,后面为fileprovider固定值,使用包名便于区分
android:exported="false"//是否支持其它应用调用当前组件 ,要求为flase
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"//固定值
android:resource="@xml/provider_paths" />//在res目录下定义的filepaths.xml文件,名字可以自定义
</provider>
2.配置XML文件
在res下创建xml文件夹,并创建filepaths.xml文件,名字可以自定义
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
path="com.example.picturedemo" name="images" />
<root-path
name="root_path"
path="." />
</paths>
3.Activity里注释写的很清楚,就不多描述了
public class TextActivityTwo extends Activity implements View.OnClickListener {
private ImageView img;
private Button popBtn;
private PopupWindow popupWindow;
private Button pop_img;
private Button pop_file;
private Button pop_cancle;
//相册请求码
private static final int ALBUM_REQUEST_CODE = 1;
//相机请求码
private static final int CAMERA_REQUEST_CODE = 2;
//剪裁请求码
private static final int CROP_SMALL_PICTURE = 3;
//调用照相机返回图片文件
private File tempFile;
//最后显示的图片文件
private String mFile;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
1);
initPopWindow();
initView();
}
private void initView() {
img = findViewById(R.id.image);
popBtn = findViewById(R.id.btn);
popBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//第一个参数是要将PopupWindow放到的View,第二个参数是位置,第三第四是偏移值
popupWindow.showAtLocation(popBtn, Gravity.BOTTOM, 0, 0);
}
});
}
public void initPopWindow() {
View popView = getLayoutInflater().inflate(R.layout.pop_btn, null);
popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pop_img = (Button) popView.findViewById(R.id.pop_img);
pop_file = (Button) popView.findViewById(R.id.pop_file);
pop_cancle = (Button) popView.findViewById(R.id.pop_cancle);
pop_img.setOnClickListener(this);
pop_file.setOnClickListener(this);
pop_cancle.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pop_img:
//关闭popupWindow
popupWindow.dismiss();
getPicFromCamera();//拍照
break;
case R.id.pop_file:
popupWindow.dismiss();
getPicFromAlbm();//从系统相册选择
break;
case R.id.pop_cancle:
popupWindow.dismiss();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case CAMERA_REQUEST_CODE: //调用相机后返回
if (resultCode == RESULT_OK) {
//用相机返回的照片去调用剪裁也需要对Uri进行处理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(this, getPackageName()+".provider", tempFile);
startPhotoZoom(contentUri);//开始对图片进行裁剪处理
} else {
startPhotoZoom(Uri.fromFile(tempFile));//开始对图片进行裁剪处理
}
}
break;
case ALBUM_REQUEST_CODE: //调用相册后返回
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
startPhotoZoom(uri); // 开始对图片进行裁剪处理
}
break;
case CROP_SMALL_PICTURE: //调用剪裁后返回
if (intent != null) {
// 让刚才选择裁剪得到的图片显示在界面上
Bitmap photo =BitmapFactory.decodeFile(mFile);
img.setImageBitmap(photo);
} else {
Log.e("data","data为空");
}
break;
}
}
public String saveImage(String name, Bitmap bmp) {
File appDir = new File(Environment.getExternalStorageDirectory().getPath());
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = name + ".png";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 裁剪图片方法实现
*
* @param uri
*/
protected void startPhotoZoom(Uri uri) {
if (uri == null) {
Log.i("tag", "The uri is not exist.");
}
// tempUri = uri;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("return-data", false);
File out = new File(getPath());
if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs();
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(intent, CROP_SMALL_PICTURE);
}
//裁剪后的地址
public String getPath() {
//resize image to thumb
if (mFile == null) {
mFile = Environment.getExternalStorageDirectory() + "/" +"wode/"+ "outtemp.png";
}
return mFile;
}
/**
* 从相机获取图片
*/
private void getPicFromCamera() {
//用于保存调用相机拍照后所生成的文件
tempFile = new File(Environment.getExternalStorageDirectory().getPath(), System.currentTimeMillis() + ".png");
//跳转到调用系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//判断版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果在Android7.0以上,使用FileProvider获取Uri
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this, getPackageName()+".provider", tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
Log.e("getPicFromCamera", contentUri.toString());
} else { //否则使用Uri.fromFile(file)方法获取Uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
}
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
/**
* 从相册获取图片
*/
private void getPicFromAlbm() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
}
参考文章:https://blog.csdn.net/z740852294/article/details/77470933
源码下载地址:https://download.csdn.net/download/zhang106209/10374401