1. obtain storage device directory :/ SDCARD (under normal circumstances)
SDPATH = Environment.getExternalStorageDirectory () + "/";
2. judgment on the SD card folder exists: Through the File object exists () method.
/ **
* determine whether a file already exists;
*
/
public boolean checkFileExists (String filepath) {
File file = new File (SDPATH + filepath);
; return file.exists ();
}
3. Create a directory on the SD card: Through the File object mkdir ( ) method.
/ *
* Create a directory on the SD card;
* /
public File createDIR (String dirpath ) {
File dir = new File (SDPATH + dirpath);
dir.mkdir ();
return dir;
}
4. creating a file on the SD card: Through the File object createNewFile () method.
/ *
* Create a file on the SD card;
* /
public File createFile ( String filepath) throws IOException {
File file = new File (SDPATH + filepath);
; file.createNewFile ();
return file;
}
5. InputStream stream of bytes to write to the SD card file .
/ **
* be an InputStream data written to the SD card
* /
public File writeStreamToSDCard (String dirpath, String filename, InputStream input) {
File file = null;
OutputStream output = null;
try {
/ / Create a directory;
; createDIR (dirpath);
/ / create a directory to create the file;
file = createFile (dirpath + filename);
output = new FileOutputStream (file);
byte [] bt = new byte [4 * 1024];
while (input.read ( bt)! = -1) {
output.write (bt);
}
/ / flush the cache,
; output.flush ();
;} catch (IOException e) {
e.printStackTrace ();
}
; finally {
; try {
output.close () ;
}
catch (Exception e) {
e.printStackTrace ();
}
}
return file;
}
6. Access permissions:
need AndroidManifest adding:
OK in that case your user has access to the path/file in question. Are you running this application in a Linux environment? If so, I'm thinking it could be something to do with the path String.
回复删除