0

ごめん、

私は Android ファイル システムの経験がありません。ドキュメントとチュートリアルで理解するのに苦労しています。

特定の場所からアプリの外部ストレージにファイルをコピーしようとしています。

    final File filetobecopied =item.getFile();
    File path=getPrivateExternalStorageDir(mContext);
    final File destination = new File(path,item.getName());
    try 
       {copy(filetobecopied,destination);
  } 
       catch (IOException e) {Log.e("",e.toString());}

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();
    Toast.makeText(mContext,"COPIED",Toast.LENGTH_SHORT).show();
}

public File getPrivateExternalStorageDir(Context context) {
    File file = context.getExternalFilesDir(null);
    if (!file.mkdirs()) {
        Log.e("", "Directory not created");
    }
    return file;
}

次のエラーが表示されます。

09-18 10:14:04.260: E/(7089): java.io.FileNotFoundException: /storage/emulated/0/Android/data/org.openintents.filemanager/files/2013-08-24 13.18.14.jpg: open failed: EISDIR (Is a directory)
4

3 に答える 3

1

フォルダーディレクトリが作成されていないか、外部ストレージの状態がマウントされていないと思います。

ファイル操作を実行する前に、パスをサニタイズする必要があります。次のコードは、私がよく使用するサンプル コードです。

public File sanitizePath(Context context) {
        String state = android.os.Environment.getExternalStorageState();
        File folder = null;
        if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
            folder = new File(context.getFilesDir() + path);
            // path is the desired location that must be specified in your code.
        }else{
            folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path);
        }
        if (!folder.exists()){
            folder.mkdirs();
        }

        return folder;
    }

ファイル操作を実行する前にディレクトリを作成する必要がある場合は、そのことを確認してください。

これが役立つことを願っています。

編集:ちなみに、次の権限を manifest.xml ファイルに追加する必要があります

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-09-18T08:52:19.783 に答える