안드로이드 개발 질문/답변
(글 수 45,052)
웹에서 파일을 받아와서 SDcard에 저장하려고 하는대요,
검색해서 찾은 소스는 밑에 처럼 되는대요
--------------------------------------------------------------------------------
// Save the name and description of a video in a ContentValues map.
ContentValues values = new ContentValues(2);
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
// values.put(MediaStore.Video.Media.DATA, f.getAbsolutePath());
// Add a new record (identified by uri) without the video, but with the values just set.
Uri uri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
try {
File file = new File(filename);
InputStream is = new FileInputStream( file );
OutputStream os = getContentResolver().openOutputStream(uri);
byte[] buffer = new byte[4096]; // tweaking this number may increase performance
int len;
while ((len = is.read(buffer)) != -1){
os.write(buffer, 0, len);
}
os.flush();
is.close();
os.close();
} catch (Exception e) {
Log.e(TAG, "exception while writing video: ", e);
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
------------------------------------------------------------
노란부분 보시면 파일을 하나 생성해서 inputstream을 하잖아요.
근데 File 찾아보니까
File(String path)
Constructs a new file using the specified path.
새로운 파일이 저장될 어떤 경로를 써야 한다고 하던데..
/sdcard/ 를 해도 안되고...
어떤 경로를 넣어야 하는지 헷갈립니다.
아직 생성되지 않은 파일의 경로면 어떻게 설정해야 하나요? ㅜㅜ
새로운 파일이 저장될 어떤 경로를 써야 한다고 하던데..
/sdcard/ 를 해도 안되고...
어떤 경로를 넣어야 하는지 헷갈립니다.
아직 생성되지 않은 파일의 경로면 어떻게 설정해야 하나요? ㅜㅜ
2010.07.02 02:26:30
File 의 객체는 파일의 생성 여부와 상관없이 만들 수 있습니다.
File f = new File(Envorinment.getExtern~~~~().toString() + "/FILENAME");
하시면 파일 객체는 생성이 되고, 여기서 파일의 존재 여부나 디렉토리인지 파일인지
이런거 체크 해 보시고 파일이 없으면 f.createNewFile() 로 파일을 생성할 수 있습니다.