问题描述:

Currently, I have 20 magazine issues and issue 20 has been downloaded and saved into my internal memory.

When I remove issue 20 from the database (including its cover page and url links), the front page would only show 19 issues and by right, issue 20's data should be removed but it does not.

When I read issue 19, data from issue 20 appears. How do I make sure that when issue 20 is removed from database, it's downloaded data should also be removed from my app when user clicks on the refresh button?

This is how I refresh my cover images:

 /** 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();

}

}});

This is how I download my magazine covers:

 /**

* 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();

}

This is how I display my image:

 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

}

});

}

网友答案:
        File t=new File("/data/data/com.example.appname/temp/");//delete from internal memory
            File t1=new File("/mnt/sdcard/foldername/");//delete from external memory
    DeleteWholeDirectory.deleteWholeDirectory(t);
    DeleteWholeDirectory.deleteWholeDirectory(t);
public static void deleteWholeDirectory(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
        for (File child : fileOrDirectory.listFiles()) {
            child.delete();
            deleteWholeDirectory(child);
        }

    fileOrDirectory.delete();
}

the above code for delete folder

相关阅读:
Top