我嘗試做清除「緩存」的onDestroy
我建立了我的頻道應用程序。我從Google獲取JSON數據,並通過SimpleAdapter將其填充到我的ListView中。
我可以在我的SimpleAdapter中顯示圖片,我將它們保存到我的SD卡上。現在的問題是,當應用程序被殺時,我得到了一個forceclose錯誤,因爲這些圖片已經準備好了。
問題
我怎麼能delte上的onDestroy我的SD卡的 「網頁快照」 影像()?
在這裏你可以找到應用程序的代碼。提前謝謝你的幫助!
如果用戶卸載應用程序的外部存儲
代碼
ChannelActivity.java
package de.stepforward;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.util.ByteArrayBuffer;
import org.json.JSONArray;
import org.json.JSONObject;
import de.stepforward.web.ShowVideo;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ChannelActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Cache löschen wenn Applikation gekillt wird
String result = "";
String line = null;
final ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();
//get the Data from URL
try{
URL url = new URL("http://gdata.youtube.com/feeds/mobile/users/TheStepForward/uploads?alt=json&format=1");
URLConnection conn = url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//read d response till d end
while ((line = rd.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
Log.v("log_tag", "Append String " + result);
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try{
JSONObject json = new JSONObject(result);
JSONObject feed = json.getJSONObject("feed");
JSONArray entrylist = feed.getJSONArray("entry");
for(int i=0;i<entrylist.length();i++){
//Get Title
JSONObject movie = entrylist.getJSONObject(i);
JSONObject title = movie.getJSONObject("title");
String txtTitle = title.getString("$t");
Log.d("Title", txtTitle);
//Get Description
JSONObject content = movie.getJSONObject("content");
String txtContent = content.getString("$t");
Log.d("Content", txtContent);
//Get Link
JSONArray linklist = movie.getJSONArray("link");
JSONObject link = linklist.getJSONObject(0);
String txtLink = link.getString("href");
Log.d("Link", txtLink);
//Get Thumbnail
JSONObject medialist = movie.getJSONObject("media$group");
JSONArray thumblist = medialist.getJSONArray("media$thumbnail");
JSONObject thumb = thumblist.getJSONObject(2);
String txtThumb = thumb.getString("url");
Log.d("Thumb", txtThumb.toString());
//ImageLoader
String name = String.valueOf(i);
String test = loadImageFromWebOperations(txtThumb, "StepForward/Cache"+name);
//String Array daraus machen und in Hashmap füllen
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Thumb", test);
map.put("Title", txtTitle);
map.put("Content", txtContent);
map.put("Link", txtLink);
mylist.add(map);
}
//ListView füllen
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.lit,
new String[] { "Thumb","Title","Content","Link"},
new int[] { R.id.img_video,R.id.txt_title,R.id.txt_subtitle});
setListAdapter(adapter);
//OnClickLister um Youtube-Video zu öffnen
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Video-Link auslesen
Map<String, Object> map = mylist.get(position);
String link = (String) map.get("Link");
Log.d("Link", link);
//Link übergeben, Activity starter
final Intent Showvideo = new Intent(ChannelActivity.this, ShowVideo.class);
Showvideo.putExtra("VideoLink", link);
startActivity(Showvideo);
}
});
}catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
}
//Path-Loader
public static String loadImageFromWebOperations(String url, String path) {
try {
InputStream is = (InputStream) new URL(url).getContent();
System.out.println(path);
File f = new File(Environment.getExternalStorageDirectory(), path);
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
try {
byte[] b = new byte[100];
int l = 0;
while ((l = is.read(b)) != -1)
fos.write(b, 0, l);
} catch (Exception e) {
}
return f.getAbsolutePath();
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
}
其不需要的,因爲它的形象媒體鏈接退出,因爲該ID想清除的onDestroy的chached文件。 – safari
有些奇怪的... – user370305