Commit 65774c88 by app_dev@sobot.com

sobot picture 优化更新到0.2

parent 93511f3a
......@@ -16,9 +16,6 @@ dependencies {
compileOnly 'com.github.bumptech.glide:glide:3.8.0'
compileOnly 'com.squareup.picasso:picasso:2.5.2'
api 'com.sobot.chat:sobotsupport-glidev4:2.0'
//如果主项目中使用的是glide,那么需要在加上
//api 'com.sobot.chat:sobotsupport-glidev4:2.0'
}
//添加发布到mavenCentral脚本
......
......@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
ext {
PUBLISH_GROUP_ID = "com.sobot.library" //项目包名
PUBLISH_ARTIFACT_ID = 'picture' //项目名
PUBLISH_VERSION = '1.0' //版本号
PUBLISH_VERSION = '0.2' //版本号
}
......
package com.sobot.pictureframe;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.view.WindowManager;
import android.widget.ImageView;
import com.sobot.chat.imageloader.SobotGlideV4ImageLoader;
import com.sobot.chat.imageloader.SobotImageLoader;
//图片显示工具类
public class SobotBitmapUtil {
private static SobotImageLoader sImageLoader;
......@@ -67,4 +73,24 @@ public class SobotBitmapUtil {
}
}
@SuppressWarnings("deprecation")
public static Bitmap compress(String filePath, Context context, boolean isCamera) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || Environment.isExternalStorageLegacy() || isCamera) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;// 设置后decode图片不会返回一个bitmap对象,但是会将图片的信息封装到Options中
BitmapFactory.decodeFile(filePath, options);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
options.inSampleSize = SobotPictureUtils.calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
} else {
return SobotPictureUtils.getBitmapFromUri(context, SobotPictureUtils.getImageContentUri(context, filePath));
}
}
}
\ No newline at end of file
package com.sobot.pictureframe;
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.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import java.io.File;
import java.io.FileDescriptor;
public class SobotPictureUtils {
public static Bitmap getBitmapFromUri(Context context, Uri uri) {
try {
ParcelFileDescriptor parcelFileDescriptor =
context.getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Uri getImageContentUri(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;
}
}
}
/**
* 计算采样大小
*
* @param options 选项
* @param reqWidth 最大宽度
* @param reqHeight 最大高度
* @return 采样大小
*/
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment