Android优化Glide和存储映像
我正在使用Glide下载来自web服务结果的图像。之后,我将图像存储在外部存储器中,下次如果文件存在,我不会下载图像,我只会在设备中搜索图像并将其显示在Gridview中。但是当我滚动时,Android优化Glide和存储映像,android,image,optimization,storage,android-glide,Android,Image,Optimization,Storage,Android Glide,我正在使用Glide下载来自web服务结果的图像。之后,我将图像存储在外部存储器中,下次如果文件存在,我不会下载图像,我只会在设备中搜索图像并将其显示在Gridview中。但是当我滚动时,new File()会使我的Gridview变慢,并且我对每个图像和每个滚动都有一个警告: 未能确保目录:/storage/sdcard1/Android/data/com.my.package/files/Pictures 如何优化新文件()行或将图像存储在其他目录中?并删除此警告 注意:当我不在设备中保存图
new File()
会使我的Gridview变慢,并且我对每个图像和每个滚动都有一个警告:
未能确保目录:/storage/sdcard1/Android/data/com.my.package/files/Pictures
如何优化新文件()
行或将图像存储在其他目录中?并删除此警告
注意:当我不在设备中保存图像时,我的Gridview非常平滑,但我必须保存它们:/。
适配器getView()
@覆盖
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图;
最终图像视图;
最终文本视图图例;
//软显示器
if(convertView==null){
视图=MLAYOUTIONFLATER.充气(R.layout.thumbnail_gallery,parent,false);
}否则{
视图=转换视图;
}
//获取布局项(图像和图例)
imageView=(imageView)view.findViewById(R.id.iv_缩略图);
图例=(TextView)view.findViewById(R.id.text\u thumb);
//不要显示旧图像
imageView.setImageDrawable(空);
//获取文件路径
//**************************************************\\
//********这条线使我的gridview变得滞后*********\\
//****************************************************\\
final File File=新文件(mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES),mpinfo.get(position.getFilename());
//获取下载路径映像
字符串pic=Globals.SERVER\u NAME+Globals
.ACCOUNT\u SERVER\u PATH+mpinfo列表
.get(位置).getFolderPath()+“/”+
addSuffix(mpinfo.get(position.getFilename(),“-thumb”);
//检查文件是否存在
如果(!file.exists()){
//创建一个简单的滑翔目标
SimpleTarget SimpleTarget=新SimpleTarget(){
@凌驾
public void onResourceReady(最终位图资源,GlideAnimation){
//线程来保存图像
新线程(newrunnable()){
@凌驾
公开募捐{
试一试{
//图像保存
FileOutputStream fileOutput=新的FileOutputStream(文件);
resource.compress(Bitmap.CompressFormat.JPEG,100,fileOutput);
fileOutput.close();
}捕获(例外e){
e、 printStackTrace();
}
}
}).start();
setImageBitmap(资源);
//设置图例
if(mpinfo.get(position).getFilename()!=null){
legend.setText(mpinfo.get(position.getLegend());
}
}
};
//文件不存在,请保存图像
使用(mContext)滑动
.load(图片)
.asBitmap()
.进入(单纯形);
}否则{
//文件存在,然后显示它
使用(mContext)滑动
.load(文件)
.进入(图像视图);
}
返回视图;
}
在设备中存储图像的目的只是为了下次显示它们。对吗?由于您正在使用Glide,此功能已在Glide中完成。Glide将图像保存在缓存中,而不是设备中。对。目的是避免重新下载。你也需要这个。是吗?是的,我想这样做,所以滑动自动保存所有图像?
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
final ImageView imageView;
final TextView legend;
// Soft display
if(convertView == null){
view = mLayoutInflater.inflate(R.layout.thumbnail_gallery, parent, false);
}else{
view = convertView;
}
// Get layout item (Image and Legend)
imageView = (ImageView) view.findViewById(R.id.iv_thumbnail);
legend = (TextView) view.findViewById(R.id.text_thumb);
// Do not display old images
imageView.setImageDrawable(null);
// Get the file path
//**************************************************\\
//******** This line make my gridview laggy *********\\
//****************************************************\\
final File file = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES), mPInfoList.get(position).getFilename());
// Get the download path image
String pic = Globals.SERVER_NAME+Globals
.ACCOUNT_SERVER_PATH+mPInfoList
.get(position).getFolderPath()+"/"+
VgzTools.addSuffix(mPInfoList.get(position).getFilename(), "-thumb");
// Check file exist
if(!file.exists()){
// Create a Simple Target for Glide
SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(final Bitmap resource, GlideAnimation glideAnimation) {
// Thread to save the image
new Thread(new Runnable() {
@Override
public void run() {
try {
// Image saving
FileOutputStream fileOutput = new FileOutputStream(file);
resource.compress(Bitmap.CompressFormat.JPEG, 100, fileOutput);
fileOutput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
imageView.setImageBitmap(resource);
// Set the legend
if (mPInfoList.get(position).getFilename() != null) {
legend.setText(mPInfoList.get(position).getLegend());
}
}
};
// File doesn't exist Save image
Glide.with(mContext)
.load(pic)
.asBitmap()
.into(simpleTarget);
}else{
// File exist then display it
Glide.with(mContext)
.load(file)
.into(imageView);
}
return view;
}