2014-11-24 108 views
-2

我写这段代码用于下载我的android应用程序中的一些文件,但现在下载的文件在根目录下没有任何特殊的顺序,它看起来非常糟糕!所以我想知道如何为我的应用程序的下载文件添加一个文件夹?如何为我的应用程序创建数据文件夹?

码字行解释输出的路径:

OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + downloadedfileName); 

这里是我的代码:

private class DownloadFileFromURL extends AsyncTask<String, String, String> { 
     private String downloadedfileName; 
     private ProgressDialog pDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      Toast.makeText(getApplicationContext(), "بارگذاری...", Toast.LENGTH_SHORT).show(); 
      pDialog = new ProgressDialog(DownloadActivity.this); 
      pDialog.setMessage("کتاب در حال بارگذاري، لطفا منتظر بمانيد..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      pDialog.dismiss(); 
      AlertDialog.Builder builder = new AlertDialog.Builder(DownloadActivity.this) 
      .setTitle("دانلود کتاب با موفقیت انجام شد") 
      .setMessage("از دانلود شما سپاس‌گذاریم.") 
      .setNeutralButton("خواهش میکنم", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
        Intent i = new Intent(DownloadActivity.this, LibActivity.class); 
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        DownloadActivity.this.startActivity(i); 
        DownloadActivity.this.finish(); 
       } 
      }); 
    builder.create().show(); 
     } 
     public void setDownloadedFileName(String downloadedfileName){ 
      this.downloadedfileName = downloadedfileName; 
     } 

     /** 
     * Downloading file in background thread 
     * */ 
     @Override 
     protected String doInBackground(String... surl) { 
      int count; 
      try { 
       URL url = new URL(surl[0]); 
       URLConnection conection = url.openConnection(); 
       conection.connect(); 

       // this will be useful so that you can show a typical 0-100% 
       // progress bar 
       int lenghtOfFile = conection.getContentLength(); 

       // download the file 
       InputStream input = new BufferedInputStream(url.openStream(), 
         8192); 

       // Output stream 
       //OutputStream output = new FileOutputStream(Environment 
       //  .getExternalStorageDirectory().toString() 
       //  + "/data/" + downloadedfileName); 
       OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/ketabha/" + downloadedfileName); 

       byte data[] = new byte[1024]; 

       long total = 0; 

       while ((count = input.read(data)) != -1) { 
        total += count; 
        // publishing the progress.... 
        // After this onProgressUpdate will be called 
        // publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 

        // writing data to file 
        output.write(data, 0, count); 
       } 

       // flushing output 
       output.flush(); 

       // closing streams 
       output.close(); 
       input.close(); 

      } catch (Exception e) { 
       Log.e("Error: ", e.getMessage()); 
      } 

      return null; 
     } 

    } 
+1

可能重复[无法在Android设备上的外部存储中创建文件夹](http://stackoverflow.com/questions/8387252/cant-create-a-folder-in-external-storage-on-android-device ) – mvillar 2014-11-24 13:58:02

+0

@mvillar没有亲爱的我在我的清单中有这样的权限。坦克你 – 2014-11-24 14:01:43

回答

1

一样简单更改为您喜欢的目录:

OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/ANYFOLDER/ANOTHERFOLDER/" + downloadedfileName); 

并记住,在第一个创建该目录与此代码:

File dir = new File(yourdirectory); 
if(!dir.exists()){ 
    dir.mkdirs(); 
} 

上面的代码工作,如果你加入你的AndroidManifest.xml文件相关的清单:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

和更多信息,请参见下面的链接:

How to create directory automatically on SD card
Create folder in Android
can't create a folder in external storage on android device

+0

谢谢你的回答。我改变了这条道路,但没有奏效! – 2014-11-24 14:02:57

+0

@NiloofarHakkaki如何添加?请提供完整的代码?我建议你先用'onPreexecute'方法创建目录。 – 2014-11-24 14:05:20

+0

我正在根文件中下载文件,以便您知道我拥有清单中的所有权限。 – 2014-11-24 14:06:14

相关问题