AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<application
...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
</application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
res文件夹下创建xml文件夹 再创建provider_paths文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="Download" />
<external-files-path
name="Download"
path="." />
<external-files-path
name="Download"
path="Download" />
<external-path
name="camera_photos"
path="." />
<external-path
name="external_files"
path="."/>
<!--<external-path path="Android/data/com.cheyifu.myapplication/" name="files_root"/>-->
<external-path path="images/" name="myimages"/>
<external-files-path name="umeng_cache" path="umeng_cache/"/>
</paths>
build.gradle(:app)加入依赖
implementation 'com.github.yalantis:ucrop:2.2.6'//裁剪
implementation 'com.github.bumptech.glide:glide:4.11.0'//加载图片
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="100dp"
android:textColor="@color/black"
android:gravity="center"
android:textSize="15dp"
android:text="相册"/>
<TextView
android:id="@+id/textView2"
android:layout_toRightOf="@+id/textView"
android:layout_width="200dp"
android:layout_height="100dp"
android:textColor="@color/black"
android:gravity="center"
android:textSize="15dp"
android:text="拍照"/>
<ImageView
android:id="@+id/image_view"
android:layout_below="@+id/textView"
android:layout_width="200dp"
android:layout_height="200dp"/>
</RelativeLayout>
//activity
public class MainActivity extends AppCompatActivity {
//对应的类 下面代码放在MainActivity类里,根据情况而定
public static final int REQ_PERMISSION_CODE = 100;
private int REQUEST_CODE_PERMISSIONS = 101; //权限
private final int REQUEST_CODE_ALBUM = 102; //相册
private final int REQUEST_CODE_CAMERA = 103; //相机
private ImageView image_view;
private Uri photoUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermissionCamera(MainActivity.this);
image_view = findViewById(R.id.image_view);
findViewById(R.id.textView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openAlbum();
}
});
findViewById(R.id.textView2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openCamera();
}
});
}
private void openAlbum() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction("android.intent.action.GET_CONTENT");
intent.addCategory("android.intent.category.OPENABLE");
startActivityForResult(intent, REQUEST_CODE_ALBUM);
}
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri
String fileName = String.format("fr_crop_%s.jpg", System.currentTimeMillis());
File cropFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName);
photoUri = FileProvider.getUriForFile(this, this.getPackageName()+".FileProvider", cropFile);//7.0
} else {
photoUri =getDestinationUri();
}
//android11以后强制分区存储,外部资源无法访问,所以添加一个输出保存位置,然后取值操作
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode){
case REQUEST_CODE_ALBUM:
doCrop(data.getData());
break;
case REQUEST_CODE_CAMERA:
//从保存的位置取值
doCrop(photoUri);
break;
case UCrop.REQUEST_CROP:
final Uri resultUri = UCrop.getOutput(data);
//Log.d("++++++++++++++bitmap=",resultUri.getPath());
Glide.with(MainActivity.this).load(resultUri).apply(RequestOptions
.bitmapTransform(new RoundedCorners(100)))
.into(image_view);
break;
}
if (resultCode == UCrop.RESULT_ERROR) {
final Throwable cropError = UCrop.getError(data);
//Log.d("++++++++++++cropError=",cropError.getMessage());
}
}
}
private void doCrop(Uri data) {
UCrop.of(data, getDestinationUri())//当前资源,保存目标位置
.withAspectRatio(1f, 1f)//宽高比
.withMaxResultSize(500, 500)//宽高
.start(this);
}
private Uri getDestinationUri() {
String fileName = String.format("fr_crop_%s.jpg", System.currentTimeMillis());
File cropFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName);
return Uri.fromFile(cropFile);
}
public static boolean checkPermissionCamera(Context context) {
try {
if (Build.VERSION.SDK_INT >= 23) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<String> permissions = new ArrayList<>();
//拍照权限
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA)) {
permissions.add(Manifest.permission.CAMERA);
}
if (permissions.size() != 0) {
ActivityCompat.requestPermissions((Activity) context,permissions.toArray(new String[0]), REQ_PERMISSION_CODE);
return false;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
04-23
1400
09-07