안드로이드 개발 질문/답변
(글 수 45,052)
검색을 통해 밑의 소스를 받아 테스트 하는데 다운 경로가 sdcard의 download 폴더로만 되더군요.
경로 지정을 어떻게 하는지 알 수 있을까요? 아직 초보라 어렵네요 ㅠ
---------------------------------------------------------------------------------------
package com.androidhuman.example.DownloaderExample;
import java.util.List;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener{
private EditText downloadUrl;
private Button addToQueueButton;
private Button cancelLatestButton;
private Button viewDownloadsButton;
private long latestId = -1;
private DownloadManager downloadManager;
private DownloadManager.Request request;
private Uri urlToDownload;
private BroadcastReceiver completeReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "다운로드가 완료되었습니다.",Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadUrl = (EditText)findViewById(R.id.downloadUrl);
addToQueueButton = (Button)findViewById(R.id.addQueueButton);
cancelLatestButton = (Button)findViewById(R.id.cancelDownloadButton);
viewDownloadsButton = (Button)findViewById(R.id.viewDownloadsButton);
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
addToQueueButton.setOnClickListener(this);
cancelLatestButton.setOnClickListener(this);
viewDownloadsButton.setOnClickListener(this);
}
@Override
public void onResume(){
super.onResume();
IntentFilter completeFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(completeReceiver, completeFilter);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.addQueueButton:
urlToDownload = Uri.parse(downloadUrl.getText().toString());
List<String> pathSegments = urlToDownload.getPathSegments();
request = new DownloadManager.Request(urlToDownload);
request.setTitle("다운로드 예제");
request.setDescription("항목 설명");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, pathSegments.get(pathSegments.size() - 1));
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();
latestId = downloadManager.enqueue(request);
downloadUrl.setText("");
break;
case R.id.cancelDownloadButton:
downloadManager.remove(latestId);
break;
case R.id.viewDownloadsButton:
startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
break;
}
}
@Override
public void onPause(){
super.onPause();
unregisterReceiver(completeReceiver);
}
}
2012.04.30 14:22:08
소스 중간중간 복사해서 올려드렸으니 참고하세요.
디렉터리 유/무 확인 없으면 mkdir로 디렉터리 생성 ..이런건 생략했어요..
byte[] buffer = new byte[1024];
URL u = new URL("http://aa.com/aa.zip);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.setUseCaches(true);
c.connect();
InputStream in = c.getInputStream();
FileOutputStream f = new FileOutputStream(new File("/data/data/경로", "파일명"));
while ( (len = in.read(buffer, 0, 1024)) > 0 ) {
f.write(buffer, 0, len);
}
f.close();
c.disconnect();




DownloadManager 클래스에 경로가 잇겟네요