大家好我开始开发一个Android应用程序,我有点困惑,哪个是最好的方法来实现它
当活动开始时它从数据库中获取数据(sqlite),数据库返回一个数据列表,这个数据列表包含文本和图像的URL。
所以现在我想要一旦活动开始,列表视图将填充文本数据和图像。
如果不存在,图像将首先在本地搜索,然后在完成提取后将从网址中提取图像,图像将被添加到其各自的视图中并存储在设备上
下载后,这些图片应保留在设备上,直到卸载该应用
一旦我们开始活动,列表视图就会填充来自本地数据库的数据,但图片需要时间下载
所以我尝试了异步图像加载器,但问题是如果活动已启动且设备未连接到网络图像不会显示但在此之后如果我们连接到互联网这些图像将无法显示
是他们处理图像的任何更简单的方法,这样如果我将来需要删除这些图像,我可以。如果有人可以提供任何项目或可用库的参考来处理这些任务.Thnx提前
答案 0 :(得分:0)
我不是你加载图像列表的方式,但我建议你使用picasso库来加载图像: 对于网址:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
for Resource:
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
从文件加载:
Picasso.with(context).load(new File(...)).into(imageView2);
更多详情请点击此处:http://square.github.io/picasso/
答案 1 :(得分:0)
下载图像并保存到磁盘:
new ImageDownloader().execute(downloadUrl);
private class ImageDownloader extends AsyncTask {
@Override
protected String doInBackground(String... param) {
// TODO Auto-generated method stub
return downloadBitmap(param[0]);
}
@Override
protected void onPreExecute() {
Log.i("Async-Example", "onPreExecute Called");
}
@Override
protected void onPostExecute(String result) {
Log.i("Async-Example", "onPostExecute Called");
Bitmap myBitmap = BitmapFactory.decodeFile(result));
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
simpleWaitDialog.dismiss();
}
private String downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();
// decoding stream data back into image Bitmap that android understands
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return SaveBitmapToDir(bitmap);
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// You Could provide a more explicit error message for IOException
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}
return null;
}
private String SaveBitmapToDir(Bitmap bmp)
{
FileOutputStream out = null;
File file = null;
try {
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy_hhmmss");
String dateTime = s.format(new Date());
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
file = new File(path, "MyApp_"+dateTime+".jpg");
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
要存储键值对,我建议您使用android的共享首选项来存储映像名称和磁盘上的位置。 这是Shared Preference
的教程将值存储在共享首选项中:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("image name","image path oon disk");
editor.apply();
从共享首选项中检索值:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String imagePath = preferences.getString("image name","");
if(!imagePath.equalsIgnoreCase(""))
{
//your action here with the image path
}