2012-09-20 125 views
0

我嘗試使用phonegap ui向我的android手機寫入文件。 我已經給了寫權限,我試圖通過使用getExternalStorageDirectory()並通過給絕對路徑。但仍然無法寫出來。 S1的是,我在外部存儲外部存儲設備上的文件寫入不起作用

Environment.getExternalStorageState(); 
//File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"/Android/"); 
       File file = new File("/mnt/sdcard"+ File.separator + "Android" + File.separator); 


       if (!file.exists()) { 
        if (!file.mkdirs()) { 
         Log.e("TravellerLog :: ", "Problem creating Folder"); 

        } 
       } 
       Environment.getExternalStorageState(); 
       File outputFile = new File(file, s1); 
       FileOutputStream fileoutputstream = new FileOutputStream(outputFile); 
       byte abyte0[] = new byte[1024]; 
       for (int i = 0; (i = inputstream.read(abyte0)) > 0;) 
        fileoutputstream.write(abyte0, 0, i); 

       fileoutputstream.close(); 
       inputstream.close(); 
+0

您已經添加外部存儲權限清單文件??? – magirtopcu

+0

@ user1645941:是的..我已經包含在我的清單文件中 – karthick

回答

0

我寫了寫一個文件到外部存儲的快速工作演示我寫的文件的名稱。

如果這仍然不起作用,它可能是一個phonegap特定的問題。

希望這有助於:

InputStream is = null; 
    OutputStream os = null; 
    byte[] buffer = new byte[2048]; 
    int bytes_read = 0; 
    File inputFile = new File("/init.rc"); 
    File outputFile = new File(Environment.getExternalStorageDirectory() + "/testfile"); 
    try 
    { 
     is = new FileInputStream(inputFile); 
     os = new FileOutputStream(outputFile); 

     while ((bytes_read = is.read(buffer)) != -1) 
     { 
      os.write(buffer, 0, bytes_read); 
     } 

    } 
    catch (Exception ignore) {} 
    finally 
    { 
     try 
     { 
      is.close(); 
     } 
     catch (Exception ignore) {} 

     try 
     { 
      os.close(); 
     } 
     catch (Exception ignore) {} 
    } 

    if (outputFile.exists()) 
    { 
     Toast.makeText(this, "Success!", Toast.LENGTH_LONG).show(); 
    } 
    else 
    { 
     Toast.makeText(this, "Failure!", Toast.LENGTH_LONG).show(); 
    } 
+0

謝謝你我會嘗試你的代碼,看看 – karthick

相關問題