目前,我有 20 期杂志,第 20 期已下载并保存到我的内存中。
当我从数据库中删除第 20 期(包括其封面页和 url 链接)时,首页只会显示 19 期,并且正确地,第 20 期的数据应该被删除,但它没有。
当我阅读第 19 期时,出现了第 20 期的数据。如何确保从数据库中删除问题 20 时,当用户单击刷新按钮时,下载的数据也应从我的应用程序中删除?
这就是我刷新封面图片的方式:
/** REFRESH BUTTON CLICKED **/
ImageButton refresh = (ImageButton) findViewById(R.id.btn_refresh);
refresh.setVisibility(Button.VISIBLE);
refresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
deleteCovers();
urlList.clear();
thumbnailList.clear();
previewList.clear();
File strPath = new File(Environment.getExternalStorageDirectory() + folderName+"Covers");
if(!strPath.exists())
{
new downloadCoverImage().execute();
}
}});
这是我下载杂志封面的方式:
/**
* Background Async Task to Load all product by making HTTP Request
* */
class downloadCoverImage extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(Store.this);
progressDialog.setMessage("Loading.." + "\n" + "加载中..");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
/**
* getting all magazines from url
* */
protected String doInBackground(String... args) {
URL myFileUrl = null;
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_magazine, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
mag = json.getJSONArray(TAG_MAGAZINE);
for (int i = 0; i < mag.length(); i++) {
JSONObject c = mag.getJSONObject(i);
// Storing each json item in variable
String magLink = c.getString(TAG_MAGAZINE_URL);
String magThumb = c.getString(TAG_MAGAZINE_THUMBNAIL);
urlList.add(magLink);
thumbnailList.add(magThumb);
//System.out.println(thumbnailList);
}
}
else {
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < thumbnailList.size(); i ++)
{
thumbnail = thumbnailList.get(i).toString();
Log.d("thumbnail", thumbnail);
Log.d("i value",String.valueOf(i));
try {
myFileUrl = new URL(thumbnail); // RETRIEVE IMAGE URL
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream in = conn.getInputStream();
Log.i("im connected", "Download");
bmImg = BitmapFactory.decodeStream(in);
File filename;
try {
// GET EXTERNAL STORAGE, SAVE FILE THERE
File storagePath = new File(Environment.getExternalStorageDirectory(),folderName+"/Covers");
storagePath.mkdirs();
filename = new File(storagePath + "/cover"+i+".jpg");
FileOutputStream out = new FileOutputStream(filename);
bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(),filename.getAbsolutePath(), filename.getName(),
filename.getName());
// displayImage();
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
progressDialog.dismiss();
displayImage();
}
这就是我显示图像的方式:
public void displayImage()
{
mStringList= new ArrayList<String>();
File strPath = new File(Environment.getExternalStorageDirectory() + folderName+"/Covers");
int lists = strPath.listFiles().length;
Log.d("number of items in /Futsing/Covers ",String.valueOf(lists));
File yourDir = new File(strPath, "");
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
String v = strPath + "/" + name;
mStringList.add(v);
}
}
mImageIds = new String[mStringList.size()];
mImageIds = mStringList.toArray(mImageIds);
for(int i = 0; i < mImageIds.length ; i++){
//Log.d("string is",(mImageIds[i]));
}
coverFlow = (CoverFlow) findViewById(R.id.coverFlow1);
coverFlow.setAdapter(new ImageAdapter(this));
coverImageAdapter = new ImageAdapter(this);
coverFlow.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(process==false)
{
adapter.clear();
File sdCard1 = Environment.getExternalStorageDirectory();
File dir1 = new File (sdCard1.getAbsolutePath() + folderName+"/issue"+issueNumber+"/");
/** IF FILE EXISTS **/
if(dir1.exists())
{
Intent intent = new Intent();
intent.setClass(Store.this, Reader.class);
intent.putExtra("issue", issueNumber);
startActivityForResult(intent, GET_INTENT_CODE);
}
else
{
new LoadPreview().execute();
}
}else{
// nothing to do here it means the process is running
}
}
}
);
coverFlow.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, final int position, long i)
{
pos=position;
issueNumber = (mImageIds.length-1 - position)+1;
adapter.clear();
buttonsCheck();
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}