我将图像保存到缓存中,然后检索它们。
我将一些url字符串保存到SharedPreferences,然后在活动为Create()时将它们拉出来。
问题在于我运行此方法..
public void getImagesfromCache(){
ImageAdapter adapter = new ImageAdapter();
String[] cachImages = myRemoteImages; //I set the CacheImages String[] here
try {
URL aURL2 = null;
aURL2 = new URL(cachImages[position]); //I get a MalformedURLException here
URI imageCacheUri = null;
try {
imageCacheUri = aURL2.toURI();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(new File(new File(this.getApplicationContext().getCacheDir(), "thumbnails"),"" + imageCacheUri.hashCode()).exists()){
Log.v("FOUND", "Images found in cache! Now being loaded!");
String cacheFile = this.getApplicationContext().getCacheDir() +"/thumbnails/"+ imageCacheUri.hashCode();
ImageView i = new ImageView(this.getApplicationContext());
FileInputStream fis = null;
try {
fis = new FileInputStream(cacheFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
i.setImageBitmap(bm);
putBitmapInDiskCache(imageCacheUri, bm);
Log.v("Loader", "Image saved to cache");
((Gallery) findViewById(R.id.gallery))
.setAdapter(new ImageAdapter(MainMenu.this));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
创建活动时,我将每个网址设置为共享偏好中的变量。
SharedPreferences app_preferences =
this.getSharedPreferences("images_urls", MODE_WORLD_READABLE);
imageUrl = app_preferences.getString("URL1", "Does not exist in preference");
imageUrl2 = app_preferences.getString("URL2", "Does not exist in cache");
imageUrl3 = app_preferences.getString("URL3", "Does not exist in preference");
imageUrl4 = app_preferences.getString("URL4", "Does not exist in preference");
Log.e("Preference", imageUrl + imageUrl2 + imageUrl3 + imageUrl4);
如你所见,我记录它以确保它拉动的东西。它在调试中显示我最初保存的所有URL。
当从SharePreferences中提取字符串时,我在oncreate中调用getimagesfromCache(),
那么为什么我在那条线上得到malformedURLException?
编辑:这是我用于远程图像的代码....
并且位置..
int position;
public String [] myRemoteImages = {imageUrl,imageUrl2,imageUrl3,imageUrl4};
我有一个图像适配器......我使用BaseAdapter内部设置位置
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(this.myContext);
try {
URL aURL = new URL(myRemoteImages[position]);
编辑:我如何在imageAdapter的getView()中使用int位置?还是我需要它?