刷新数据后删除文件夹

时间:2013-01-28 07:43:42

标签: java android directory

目前,我有20个杂志问题,已下载问题20并保存到我的内存中。

当我从数据库中删除问题20(包括其封面和网址链接)时,首页只会显示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

             }
         });
    }

1 个答案:

答案 0 :(得分:0)

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

以上删除文件夹的代码