Android7.0呼叫系統拍照、系統相簿(圖片壓縮、檢視原圖、許可權處理)
阿新 • • 發佈:2019-02-12
效果以及需求
檢視原圖可點選放大,可兩指放大等
1.PopupWindow的簡單使用
PopupWindow最基本的三個條件是一定要設定的:View contentView,int width, int height
showAtLocation 視窗展示的位置
setOutsideTouchable(true)點選外面關閉PopupWindow,一定要在showAtLocation之前呼叫
View contentView = LayoutInflater.from(this).inflate(R.layout.pop_pic_photo, null);
popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
TextView load = contentView.findViewById(R.id.load);
load.setOnClickListener(this);
2.Android6.0許可權申請
// 現在清單檔案進行宣告
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
// 許可權申請
ActivityCompat.requestPermissions(,new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode );
// 處理許可權回撥
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// 相機許可權授予
grantResults[0] == PackageManager.PERMISSION_GRANTED;
// 儲存許可權授予
grantResults[1] == PackageManager.PERMISSION_GRANTED;
}
3.呼叫系統相簿
// 開啟系統相簿
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 123);
// onActivityResult進行回撥得到圖片路徑
if (requestCode == 123 && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
// 圖片路徑
String imagePath = c.getString(columnIndex);
c.close();
}
4.呼叫系統相機
protected void open_camra() {
Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imageFileName = System.currentTimeMillis() + ".png";
String path = Environment.getExternalStorageDirectory();
// 定義生成圖片的路徑(全域性)
StringpicPath = path+imageFileName;
Uri mImageCaptureUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //android7.0以上
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, new File(path, imageFileName).getAbsolutePath());
mImageCaptureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
} else {
mImageCaptureUri = Uri.fromFile(new File(path, imageFileName));
}
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(takeIntent, 456);
}
// onActivityResult進行回撥得到圖片路徑
if (requestCode == 456 && resultCode == Activity.RESULT_OK) {
// 圖片路徑
StringpicPath
}
Luban.with(this)
.load(photos) // 傳人要壓縮的圖片列表
.ignoreBy(100) // 忽略不壓縮圖片的大小
.setTargetDir(getPath()) // 設定壓縮後文件儲存位置
.setCompressListener(new OnCompressListener() { //設定回撥
@Override
public void onStart() {
// TODO 壓縮開始前呼叫,可以在方法內啟動 loading UI
}
@Override
public void onSuccess(File file) {
// TODO 壓縮成功後呼叫,返回壓縮後的圖片檔案
}
@Override
public void onError(Throwable e) {
// TODO 當壓縮過程出現問題時呼叫
}
}).launch(); //啟動壓縮
6.圖片展示
protected void showImage(File file, ImageView img) {
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
img.setImageBitmap(bitmap);
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
// root build.gradle
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
// module build.gradle
compile 'com.github.chrisbanes:PhotoView:2.1.3'
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/photo_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
photoView.setImageBitmap(BitmapFactory.decodeFile(path));