序文
プロジェクトに遭遇した、写真のシーンをアップロードするユーザーが必要。結果は、あなたが2000以上3000またはそれ以上の絵、偉大な解像度、長さと幅を取る特に後に、ユーザーによってアップロードされた画像5メガバイト以上。結果の後、遅い、インターネットからこれらのイメージをロードします。
我々はアップロード前に圧縮されなければならなかった後そう、その後、アップロードします。
fileutilsファイルの書き込み
/**
* desc :
* author : stone
* email : [email protected]
* time : 2019/5/29 9:19
*/
public class FileUtils {
//获取指定文件目录的内部文件(路径)
public static File getAlbumStorageDir(Context context, String directory) {
File file;
//有sdcard
if (TextUtils.equals(Environment.getExternalStorageState(), Environment.MEDIA_MOUNTED)) {
file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), directory);
} else {
file = new File(context.getCacheDir(), directory);
}
if (!file.mkdirs()) {
LogUtils.e("ExtensionSignActivity", "Directory not created");
}
return file;
}
//保存 jpg/png的 Bitmap
public static String saveBmp2Local(Context context, Bitmap bmp, String picName, boolean isPng) {
// 声明文件对象
File file = null;
// 声明输出流
FileOutputStream outStream = null;
try {
file = new File(getAlbumStorageDir(context, LFConfig.IMG_DIRECTORY_NAME), picName + (isPng ? ".png" : ".jpg"));
// 获得输出流,如果文件中有内容,追加内容
outStream = new FileOutputStream(file);
bmp.compress(isPng ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 90, outStream);
} catch (Exception e) {
e.getStackTrace();
} finally {
try {
if (outStream != null) {
outStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (file != null) {
return file.getPath();
}
return null;
}
//保存gif。使用nio写入
public static String saveGif2Local(Context context, ByteBuffer byteBuffer, String picName) throws FileNotFoundException {
// 声明文件对象
File file = null;
// 声明输出流
FileOutputStream outStream = null;
FileChannel channel = null;
try {
file = new File(getAlbumStorageDir(context, LFConfig.IMG_DIRECTORY_NAME), picName + ".gif");
// 获得输出流,如果文件中有内容,追加内容
outStream = new FileOutputStream(file);
channel = outStream.getChannel();
//不能确定channel.write()能一次性写入buffer的所有数据
//所以通过判断是否有余留循环写入
while (byteBuffer.hasRemaining()) {
channel.write(byteBuffer);
}
} catch (Exception e) {
e.getStackTrace();
} finally {
try {
if (outStream != null) {
outStream.close();
}
if (channel != null) {
channel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (file != null) {
return file.getPath();
}
return null;
}
public static void deleteTempImages(Context context) {
com.blankj.utilcode.util.FileUtils.deleteDir(getAlbumStorageDir(context, LFConfig.IMG_DIRECTORY_NAME));
}
}
グライドロードされ、圧縮
複合rxjava位Obserableは、サブスレッドで、画像パスマップの変換を行うことができます。
ビットマップまたはGifDrawableはグライド負荷絵に変身しました。
一度、加入圧縮してローカルに保存されています。
GIFの圧縮について、まだ検討します。
/**
* 存储到本地
* @param item 原始图片路径
*/
private void compressAndSaveItem(final String item) {
String[] strArray = item.split("\\.");
if (strArray.length > 0 && strArray[strArray.length - 1].toLowerCase().equals("gif")) {//判断后缀名是否是gif
Observable.just(item)
.observeOn(Schedulers.io()) //整体运行行在子线程
.compose(RxJavaUtil.rxLife(PublishProductActivity.this)) //结合了rxlifecycle库
.map(s -> GlideApp.with(PublishProductActivity.this)
.asGif()
.load(s)
.fitCenter()
.submit()//原始分辨率
// .submit(720, 1080) //自定义分辨率
.get())
.subscribe(result -> {
int pointIndex = item.lastIndexOf(".");
int nameStartIndex = item.lastIndexOf("/") + 1;
int end;
if (pointIndex < nameStartIndex) {
end = item.length();
} else {
end = pointIndex;
}
String fileName = item.substring(nameStartIndex, end);
String path = FileUtils.saveGif2Local(PublishProductActivity.this,
result.getBuffer(), fileName + "_copy");
/*
* 有了保存的压缩图片路径 path
* do other thing
*/
}, error -> {
error.printStackTrace();
});
} else {
Observable.just(item)
.observeOn(Schedulers.io())//整体运行行在子线程
.compose(RxJavaUtil.rxLife(PublishProductActivity.this)) //结合了rxlifecycle库
.map(s -> GlideApp.with(PublishProductActivity.this)
.asBitmap()
.load(s)
.fitCenter()
.submit()//原始分辨率
// .submit(720, 1080) //自定义分辨率
.get())
.subscribe(result -> {
int pointIndex = item.lastIndexOf(".");
int nameStartIndex = item.lastIndexOf("/") + 1;
int end;
if (pointIndex < nameStartIndex) {
end = item.length();
} else {
end = pointIndex;
}
String fileName = item.substring(nameStartIndex, end);
//缩放bitmap都为原宽高的1/2
Bitmap scaleBitmap = Bitmap.createScaledBitmap(result,
result.getWidth() / 2, result.getHeight() / 2, true);
String path = FileUtils.saveBmp2Local(PublishProductActivity.this, scaleBitmap
, fileName + "_copy",
strArray[strArray.length - 1].toLowerCase().equals("png"));
/*
* 有了保存的压缩图片路径 path
* do other thing
*/
}, error -> {
error.printStackTrace();
});
}
私たちは、GIF画像は表示されません。問題が発生しました
NIOロジックの保存とあまりにもピット、GifDrawable#getBuffer()は、問題ありません。ショーに十分に保存した後、結果として得られる画像、おそらくグライドのバグのように見えますが、唯一のグライドの契約を使用していない、削除されたGIFの関連するコードとすることができます。