나는 다음 링크안드로이드 : 서버에서 이미지를 다운로드 및 장치 캐시에 저장할
하고 내가 그것을로드 한 후 이미지를 캐시 할 수있는 다음 자습서를 따랐다 겪었 서버에서내 코드 세그먼트는 다음과 같습니다
private Bitmap getBitmap(String url) {
// PhotoToLoad photoToLoad = new PhotoToLoad(url, new ImageView(a));
// String filename = photoToLoad.url;
//String filename = url;
String filename = String.valueOf(url.hashCode());
Log.v("TAG FILE :", filename);
File f = new File(cacheDir, filename);
// Is the bitmap in our cache?
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
if (bitmap != null)
return bitmap;
else {
// Nope, have to download it
try {
bitmap = BitmapFactory.decodeStream(new URL(url)
.openConnection().getInputStream());
// save bitmap to cache for later
writeFile(bitmap, f);
return bitmap;
} catch (FileNotFoundException ex) {
ex.printStackTrace();
Log.v("FILE NOT FOUND", "FILE NOT FOUND");
return null;
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
}
private void writeFile(Bitmap bmp, File f) {
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (Exception ex) {
}
}
}
나는 FileNotFoundException
을 얻고있다 :
11-12 15:19:25.495: VERBOSE/cacheDir(266): /sdcard/data/BalajeeBazaar
11-12 15:19:26.035: VERBOSE/TAG FILE :(266): -951166081
11-12 15:19:26.315: WARN/System.err(266): java.io.FileNotFoundException: /sdcard/data/BalajeeBazaar/-951166081
11-12 15:19:26.315: WARN/System.err(266): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:231)
11-12 15:19:26.315: WARN/System.err(266): at java.io.FileOutputStream.<init>(FileOutputStream.java:96)
11-12 15:19:26.315: WARN/System.err(266): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
11-12 15:19:26.315: WARN/System.err(266): at com.ecommerce.balajeebazaar.CacheImages.writeFile(CacheImages.java:160)
11-12 15:19:26.315: WARN/System.err(266): at com.ecommerce.balajeebazaar.CacheImages.getBitmap(CacheImages.java:142)
11-12 15:19:26.315: WARN/System.err(266): at com.ecommerce.balajeebazaar.CacheImages.access$0(CacheImages.java:125)
11-12 15:19:26.315: WARN/System.err(266): at com.ecommerce.balajeebazaar.CacheImages$PhotosLoader.run(CacheImages.java:77)
11-12 15:19:26.325: WARN/dalvikvm(266): threadid=17: thread exiting with uncaught exception (group=0x4001aa28)
11-12 15:19:26.325: ERROR/AndroidRuntime(266): Uncaught handler: thread Thread-10 exiting due to uncaught exception
11-12 15:19:26.325: ERROR/AndroidRuntime(266): java.lang.ClassCastException: android.graphics.Bitmap
11-12 15:19:26.325: ERROR/AndroidRuntime(266): at com.ecommerce.balajeebazaar.CacheImages$PhotosLoader.run(CacheImages.java:79)
방법이 문제를 해결하려면 나를 인도하세요?
감사합니다 ~
매니페스트에 추가 한 적이 있습니까? ? 여기에 나를 위해 일한이 프로젝트를 시도하십시오 : https://github.com/thest1/LazyList –
예 내가 그것을 추가했습니다 – Shruti