내가 가진 것 :
내가하려는 것 :
RAW folder
내 Android 프로젝트에 파일을 다운로드하고 싶습니다.
나는 익숙 AsyncTask
하고 HttpClient
있지만 파일을 다운로드하기 위해 어떤 방법 (단계)을 따라야한다?
stackoverflow에서 비슷한 질문을 검색했지만 찾을 수 없으므로 직접 질문을 게시했습니다.
"assets"또는 "/ res / raw"로 파일을 다운로드 할 수 없습니다. 그것들은 APK로 컴파일됩니다.
앱 내부 데이터 디렉토리에 파일을 다운로드 할 수 있습니다. 참조 파일 저장이 | Android 개발자 .
다운로드에 도움이되는 많은 예제와 라이브러리가 있습니다. 다음은 프로젝트에서 사용할 수있는 정적 팩토리 방법입니다.
public static void download(String url, File file) throws MalformedURLException, IOException {
URLConnection ucon = new URL(url).openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) ucon;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedInputStream bis = new BufferedInputStream(ucon.getInputStream());
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
bis.close();
}
}
그런 다음 Dropbox에서 파일을 다운로드하려면 :
String url = "https://dl.dropboxusercontent.com/u/27262221/test.txt";
File file = new File(getFilesDir(), "test.txt");
try {
download(url, file);
} catch (MalformedURLException e) {
// TODO handle error
} catch (IOException e) {
// TODO handle error
}
위의 코드는 백그라운드 스레드에서 실행되어야합니다 NetworkOnMainThreadException
. 그렇지 않으면 .
또한 AndroidManifest에서 다음 권한을 선언해야합니다.
<uses-permission android:name="android.permission.INTERNET" />
여기에서 유용한 라이브러리를 찾을 수 있습니다. https://android-arsenal.com/free
개인적으로 http-request를 권장 합니다. 다음과 같이 HttpRequest로 보관 용 파일을 다운로드 할 수 있습니다.
HttpRequest.get("https://dl.dropboxusercontent.com/u/27262221/test.txt").receive(
new File(getFilesDir(), "test.txt"));
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다