android6.0的权限机制改得跟ios类似,两个系统之间互相模仿也是好事,取长补短。在某些权限需要用到的时候,要先询问用户。
官方说明:http://developer.android.com/intl/zh-cn/about/versions/marshmallow/android-6.0-changes.html(要翻墙)
国内各大android系统对于权限的设置机制都不大一样,但大部分都是一些常用的权限默认是开启的,一些是默认关闭了,即使你在AndroidManifes文件中声明了。权限分为两类,一类是普通权限,一类是危险权限。普通权限只需在AndroidManifes文件声明即可,但是危险权限每用一次必须询问一次。如果你不进行询问和请求,那默认是不开启的,即你这项功能无法用,这也是为什么客服反馈为什么一些手机上某些功能无法用的原因,然后全组人丈二摸不着头脑,当然,这也取决于开发人员的经验。
- 普通权限(Normal Permissions)
这里就不介绍了。
- 危险权限(Dangerous Permissions)
同一组的任何一个权限被授权了,其他权限也自动被授权。例如,一旦WRITE_CONTACTS被授权了,app也有READ_CONTACTS和GET_ACCOUNTS了。
- demo
MainActivity.java
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.image);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (PermissionUtils.isCameraPermission(MainActivity.this, 0x007))
byCamera();
}
});
}
//请求权限回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 0x007:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
byCamera();
} else {
Toast.makeText(this, "拍照权限被拒绝", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private String imagePath;
public void byCamera() {
String savePath = "";
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/camera/";
File savedir = new File(savePath);
if (!savedir.exists()) {
savedir.mkdirs();
}
}
// 没有挂载SD卡,无法保存文件
if (savePath == null || "".equals(savePath)) {
System.out.println("无法保存照片,请检查SD卡是否挂载");
return;
}
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//照片命名
String fileName = timeStamp + ".jpg";
File out = new File(savePath, fileName);
Uri uri = Uri.fromFile(out);
//该照片的绝对路径
imagePath = savePath + fileName;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 0x008);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 8) {
if(imagePath!=null && resultCode == RESULT_OK){
Log.e(">>>>>>>>>>....",""+imagePath);
imageView.setImageBitmap(PermissionUtils.getBitmapByPath(imagePath,480,800));
}
}
}
}
PermissionUtils.java
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class PermissionUtils {
private static String[] PERMISSIONS_CAMERA_AND_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA};
public static boolean isCameraPermission(Activity context, int requestCode){
if (Build.VERSION.SDK_INT >= 23) {
int storagePermission = ActivityCompat.checkSelfPermission(context,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
int cameraPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
if (storagePermission != PackageManager.PERMISSION_GRANTED || cameraPermission!= PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(context, PERMISSIONS_CAMERA_AND_STORAGE,
requestCode);
return false;
}
}
return true;
}
/**
* 获取bitmap
*
* @param filePath
* @return
*/
public static Bitmap getBitmapByPath(String filePath, int w, int h) {
FileInputStream fis = null;
Bitmap bitmap = null;
try {
File file = new File(filePath);
if (file.exists()) {
fis = new FileInputStream(file);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
int originalWidth = options.outWidth;//图片原始宽度
int originalHeight = options.outHeight;//图片原始高度
if ((originalWidth == -1) || (originalHeight == -1))
return null;
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (originalWidth > originalHeight && originalWidth > w) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (originalWidth / w);
} else if (originalWidth < originalHeight && originalHeight > h) {//如果高度高的话根据宽度固定大小缩放
be = (int) (originalHeight / h);
}
if (be <= 0)
be = 1;
options.inJustDecodeBounds = false;
options.inSampleSize = be;//设置缩放比例
options.inPreferredConfig = Bitmap.Config.RGB_565;
bitmap = BitmapFactory.decodeFile(filePath, options);
}
} catch (IOException e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (Exception e) {
}
}
//return getBitmapByPath(filePath, bitmapOptions);
return compressImage(bitmap);
}
/**
* 质量压缩方法
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 90;
int bytes = baos.toByteArray().length;
while ((bytes / 1024 > 100) && (options >= 20)) { //循环判断如果压缩后图片是否大于10kb,大于继续压缩
baos.reset();//重置baos即清空baos
options -= 10;//每次都减少10
//第一个参数 :图片格式 ,第二个参数: 图片质量,100为最高,0为最差 ,第三个参数:保存压缩后的数据的流
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
bytes = baos.toByteArray().length;
}
image.recycle();
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}
}
demo源码