..需要下載的工具類 https://share.weiyun.com/5wJW6VS
拍照,使用儲存卡路徑(需要申請儲存許可權),即圖片的路徑在 儲存卡目錄下 -> 包名 -> icon資料夾下
public void takePhoto(View view) {
imgPath = FileUtils.generateImgePathInStoragePath();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//如果是6.0或6.0以上,則要申請執行時許可權,這裡需要申請拍照的許可權
requestRuntimePermission(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, new PermissionListener() {
@Override
public void onGranted() {
//開啟攝像頭,拍照完的圖片儲存在對應路徑下
openCamera(imgPath);
//DoGet();
}
@Override
public void onDenied(List<String> deniedPermissions) {
UIUtils.showToast("所需許可權被拒絕");
}
});
return;
}
openCamera(imgPath);
}
複製程式碼
開啟相簿
public void openAlbum(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, REQ_ALBUM);
}
複製程式碼
開啟攝像機
private void openCamera(String imgPath) {
// 指定呼叫相機拍照後照片的儲存路徑
File imgFile = new File(imgPath);
Uri imgUri = null;
if (Build.VERSION.SDK_INT >= 24) {
//如果是7.0或以上
imgUri = FileProvider.getUriForFile(this, UIUtils.getPackageName() + ".fileprovider", imgFile);
} else {
imgUri = Uri.fromFile(imgFile);
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(intent, REQ_TAKE_PHOTO);
}
複製程式碼
申請執行時許可權
public void requestRuntimePermission(String[] permissions, PermissionListener permissionListener) {
this.permissionListener = permissionListener;
List<String> permissionList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(permission);
}
}
if (!permissionList.isEmpty()) {
ActivityCompat.requestPermissions(this, permissionList.toArray(new String[permissionList.size()]), 1);
} else {
permissionListener.onGranted();
}
}
複製程式碼
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case RESULT_OK://呼叫圖片選擇處理成功
String zoomImgPath = "";
Bitmap bm = null;
File temFile = null;
File srcFile = null;
File outPutFile = null;
switch (requestCode) {
case REQ_TAKE_PHOTO:// 拍照後在這裡回撥
srcFile = new File(imgPath);
outPutFile = new File(FileUtils.generateImgePathInStoragePath());
outputUri = Uri.fromFile(outPutFile);
FileUtils.startPhotoZoom(this, srcFile, outPutFile, REQ_ZOOM);// 發起裁剪請求
break;
case REQ_ALBUM:// 選擇相簿中的圖片
if (data != null) {
Uri sourceUri = data.getData();
String[] proj = {MediaStore.Images.Media.DATA};
// 好像是android多媒體資料庫的封裝介面,具體的看Android文件
Cursor cursor = managedQuery(sourceUri, proj, null, null, null);
// 按我個人理解 這個是獲得使用者選擇的圖片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
// 將游標移至開頭 ,這個很重要,不小心很容易引起越界
cursor.moveToFirst();
// 最後根據索引值獲取圖片路徑
imgPath = cursor.getString(column_index);
srcFile = new File(imgPath);
outPutFile = new File(FileUtils.generateImgePathInStoragePath());
outputUri = Uri.fromFile(outPutFile);
FileUtils.startPhotoZoom(this, srcFile, outPutFile, REQ_ZOOM);// 發起裁剪請求
}
break;
case REQ_ZOOM://裁剪後回撥
// Bundle extras = data.getExtras();
if (data != null) {
// bm = extras.getParcelable("data");
if (outputUri != null) {
bm = ImageTools.decodeUriAsBitmap(outputUri);
//如果是拍照的,刪除臨時檔案
temFile = new File(imgPath);
if (temFile.exists()) {
temFile.delete();
}
//進行上傳,上傳成功後顯示新圖片,這裡不演示上傳的邏輯,上傳只需將scaleImgPath路徑下的檔案上傳即可。
//複製並壓縮到自己的目錄並壓縮
scaleImgPath = FileUtils.saveBitmapByQuality(bm, 80);
//獲取圖片名字
ImageGName();
//圖片上傳
HttpUtils.doFile("https://www.baidu.com", scaleImgPath,fileName , new Callback() {
@Override
public void onFailure(Call call, IOException e) {
L.d("11111111111","### fileName : " + fileName);
L.d("11111111111","失敗");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
L.d("33333333333","### fileName : " + fileName);
L.d("33333333333","成功");
}
});
//顯示在iv上
Glide.with(this)
.load(outputUri)
.placeholder(R.mipmap.ic_launcher)
.crossFade()
.into(ivPhoto);
// ivPhoto.setImageBitmap(bm);//顯示在iv上
}
} else {
UIUtils.showToast("選擇圖片發生錯誤,圖片可能已經移位或刪除");
}
break;
}
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0) {
List<String> deniedPermissions = new ArrayList<>();
for (int i = 0; i < grantResults.length; i++) {
int grantResult = grantResults[i];
String permission = permissions[i];
if (grantResult != PackageManager.PERMISSION_GRANTED) {
deniedPermissions.add(permission);
}
}
if (deniedPermissions.isEmpty()) {
permissionListener.onGranted();
} else {
permissionListener.onDenied(deniedPermissions);
}
}
break;
}
}
複製程式碼
public void ImageGName(){
// 建立File
File mFile = new File(scaleImgPath);
// 取得檔名
fileName = mFile.getName();
}
複製程式碼