0

私のアプリケーションには、doc/img ファイル パスをデータベースに保存する機能があります。このファイルはフォルダにあります (例: "/mnt/sdcard/MyApp/MyItem/test.png")。今私がしたいことは、このファイルを他のフォルダー (例: /mnt/sdcard/MyApp/MyItem/Today/test.png) にコピーすることです。

現在、以下のコードを使用していますが、機能していません。

private void copyDirectory(File from, File to) throws IOException {


    try {
        int bytesum = 0;
        int byteread = 0;

            InputStream inStream = new FileInputStream(from);
            FileOutputStream fs = new FileOutputStream(to);
            byte[] buffer = new byte[1444];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();

    } catch (Exception e) {
    }
}

ボタンをクリックすると、次のコードを使用します。

File sourceFile = new File(fileList.get(0).getAbsolutePath); //comes from dbs File targetFile = new File(Environment.getExternalStorageDirectory(),"MyApp/MyItem/Today/"); copyDirectory(sourceFile,targetFile, currDateStr);

なぜそれが機能しないのですか?

4

2 に答える 2

0

うん、それは動作しました、私はファイルをコピーしている間ファイル名を与えていませんでした、そして実際にエラーログを見ませんでした、それを今動作させましたおかげで。そして、はい、上記のコードは問題なく機能します。

于 2013-03-12T09:17:07.650 に答える
0

このコードは私にとってはうまく機能しています。

public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

そしてもう 1 つ、マニフェスト ファイルに*外部ストレージへの書き込み権限を追加しました。*

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-03-12T07:47:24.137 に答える