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

1 回答 1

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();
    }
于 2012-09-20T15:21:05.353 回答