我将图像保存到缓存,然后检索它们。
我将一些 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();
}
创建活动时,我将每个 url 设置为来自 sharedpreferences 的变量。
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;
公共字符串 [] 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 位置?还是我需要它?