Skip to content

Commit

Permalink
1. 适配 Android Q 版本
Browse files Browse the repository at this point in the history
2. 将裁剪图片路径改成不固定格式
  • Loading branch information
wildma committed Feb 17, 2020
1 parent f3f909e commit 445e542
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 51 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
compileSdkVersion 29
defaultConfig {
applicationId "com.wildma.wildmaselectpicture"
minSdkVersion 14
targetSdkVersion 27
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
28 changes: 18 additions & 10 deletions app/src/main/java/com/wildma/wildmaselectpicture/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;


public class MainActivity extends AppCompatActivity {
private ImageView mIvImage;
public static final String TAG = "PictureSelector";
private ImageView mIvImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -41,15 +44,20 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/*结果回调*/
if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
if (data != null) {
String picturePath = data.getStringExtra(PictureSelector.PICTURE_PATH);
mIvImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));

/*使用 Glide 加载图片,由于裁剪后的图片地址是相同的,所以不能从缓存中加载*/
/*RequestOptions requestOptions = RequestOptions
.circleCropTransform()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true);
Glide.with(this).load(picturePath).apply(requestOptions).into(mIvImage);*/
PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
Log.i(TAG, "是否裁剪: " + pictureBean.isCut());
Log.i(TAG, "原图地址: " + pictureBean.getPath());
Log.i(TAG, "图片 Uri: " + pictureBean.getUri());
if (pictureBean.isCut()) {
mIvImage.setImageBitmap(BitmapFactory.decodeFile(pictureBean.getPath()));
} else {
mIvImage.setImageURI(pictureBean.getUri());
}

//使用 Glide 加载图片
/*Glide.with(this)
.load(pictureBean.isCut() ? pictureBean.getPath() : pictureBean.getUri())
.apply(RequestOptions.centerCropTransform()).into(mIvImage);*/
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions pictureselector/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 27
compileSdkVersion 29
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 114
versionName "1.1.4"
targetSdkVersion 29
versionCode 115
versionName "1.1.5"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.wildma.pictureselector;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import java.io.File;
Expand All @@ -25,6 +27,39 @@ public static File getRootPath() {
return path;
}

/**
* 获取图片目录
*
* @return 图片目录(/storage/emulated/0/Pictures)
*/
public static File getExtPicturesPath() {
File extPicturesPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!extPicturesPath.exists()) {
extPicturesPath.mkdir();
}
return extPicturesPath;
}

/**
* 获取缓存图片的目录
*
* @param context Context
* @return 缓存图片的目录
*/
public static String getImageCacheDir(Context context) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
cachePath = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();
} else {
cachePath = context.getExternalCacheDir().getPath();
}
} else {
cachePath = context.getCacheDir().getPath();
}
return cachePath;
}

/**
* SD卡是否可用
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.wildma.pictureselector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;

/**
* Author wildma
* Github https://github.com/wildma
Expand Down Expand Up @@ -40,13 +43,13 @@ private static boolean isSpace(final String s) {
}

/**
* 从Uri中获取图片路劲
* 获取图片路径
*
* @param context
* @param uri 图片Uri
* @return
* @param context Context
* @param uri 图片 Uri
* @return 图片路径
*/
public static String getImagePathFromUri(Context context, Uri uri) {
public static String getImagePath(Context context, Uri uri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
Expand All @@ -60,4 +63,30 @@ public static String getImagePathFromUri(Context context, Uri uri) {
}
}
}

/**
* 获取图片 Uri
*
* @param context Context
* @param path 图片路径
* @return 图片 Uri
*/
public static Uri getImageUri(Context context, String path) {
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ", new String[]{path}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (new File(path).exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, path);
return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.wildma.pictureselector;

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

/**
* Author wildma
* Github https://github.com/wildma
* Date 2020/02/16
* Desc ${图片实体类}
*/
public class PictureBean implements Parcelable {

//原图地址
private String path;
//图片 Uri
private Uri uri;
//是否裁剪
private boolean isCut;

public String getPath() {
return path == null ? "" : path;
}

public void setPath(String path) {
this.path = path;
}

public Uri getUri() {
return uri;
}

public void setUri(Uri uri) {
this.uri = uri;
}

public boolean isCut() {
return isCut;
}

public void setCut(boolean cut) {
isCut = cut;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.path);
dest.writeParcelable(this.uri, flags);
dest.writeByte(this.isCut ? (byte) 1 : (byte) 0);
}

public PictureBean() {
}

protected PictureBean(Parcel in) {
this.path = in.readString();
this.uri = in.readParcelable(Uri.class.getClassLoader());
this.isCut = in.readByte() != 0;
}

public static final Creator<PictureBean> CREATOR = new Creator<PictureBean>() {
@Override
public PictureBean createFromParcel(Parcel source) {
return new PictureBean(source);
}

@Override
public PictureBean[] newArray(int size) {
return new PictureBean[size];
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.io.File;

/**
* Author wildma
* Github https://github.com/wildma
Expand Down Expand Up @@ -113,8 +117,17 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
String picturePath = PictureSelectUtils.onActivityResult(this, requestCode, resultCode, data, mCropEnabled, mCropWidth, mCropHeight, mRatioWidth, mRatioHeight);
if (!TextUtils.isEmpty(picturePath)) {
PictureBean bean = new PictureBean();
bean.setPath(picturePath);
bean.setCut(mCropEnabled);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
bean.setUri(ImageUtils.getImageUri(this, picturePath));
} else {
bean.setUri(Uri.fromFile(new File(picturePath)));
}

Intent intent = new Intent();
intent.putExtra(PictureSelector.PICTURE_PATH, picturePath);
intent.putExtra(PictureSelector.PICTURE_RESULT, bean);
setResult(RESULT_OK, intent);
finish();
}
Expand Down
Loading

0 comments on commit 445e542

Please sign in to comment.