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 に答える