android 6.0許可權開發---拍照,開啟相簿問題
阿新 • • 發佈:2019-01-08
android6.0的許可權機制改得跟ios類似,兩個系統之間互相模仿也是好事,取長補短。在某些許可權需要用到的時候,要先詢問使用者。
國內各大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原始碼