서버로부터 파일다운을 안드로이드에서 받을 수 있게하려면 어떻게해야될까요
제가지금해본건 한장밖에 다운이안되고
다운로드할 파일이름도 지정해줘야되거든요..
다른 어플처럼
리스트뷰로 서버에 잇는 파일들을 보여줘서 다운로드할 파일을 지정한다던가
여러장 받을수있게하려면 어떻게해야될까요
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
String File_Name = "123.jpeg";
String File_extend = "jpeg";
String fileURL = ""; // URL은 지워놧습니다.
String Save_Path;
String Save_folder = "/Goodcam2";
ProgressBar loadingBar;
DownloadThread dThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.downbtn);
btn.setOnClickListener(this);
loadingBar = (ProgressBar) findViewById(R.id.Loading);
// 다운로드 로를 외장메모리 사용자 지정 폴더로 함.
String ext = Environment.getExternalStorageState();
if (ext.equals(Environment.MEDIA_MOUNTED)) {
Save_Path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + Save_folder;
}
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view.getId() == R.id.downbtn) {
File dir = new File(Save_Path);
// 폴더가 존재하지 않을 경우 폴더를 만듦
if (!dir.exists()) {
dir.mkdir();
}
// 다운로드 폴더에 동일한 파일명이 존재하는지 확인해서
// 없으면 다운받고 있으면 해당 파일 실행시킴.
if (new File(Save_Path + "/" + File_Name).exists() == false) {
loadingBar.setVisibility(View.VISIBLE);
dThread = new DownloadThread(fileURL + "/" + File_Name,
Save_Path + "/" + File_Name);
dThread.start();
} else {
showDownloadFile();
}
}
}
// 다운로드 쓰레드로 돌림..
class DownloadThread extends Thread {
String ServerUrl;
String LocalPath;
DownloadThread(String serverPath, String localPath) {
ServerUrl = serverPath;
LocalPath = localPath;
}
@Override
public void run() {
URL imgurl;
int Read;
try {
imgurl = new URL(ServerUrl);
HttpURLConnection conn = (HttpURLConnection) imgurl
.openConnection();
int len = conn.getContentLength();
byte[] tmpByte = new byte[len];
InputStream is = conn.getInputStream();
File file = new File(LocalPath);
FileOutputStream fos = new FileOutputStream(file);
for (;;) {
Read = is.read(tmpByte);
if (Read <= 0) {
break;
}
fos.write(tmpByte, 0, Read);
}
is.close();
fos.close();
conn.disconnect();
} catch (MalformedURLException e) {
Log.e("ERROR1", e.getMessage());
} catch (IOException e) {
Log.e("ERROR2", e.getMessage());
e.printStackTrace();
}
mAfterDown.sendEmptyMessage(0);
}
}
Handler mAfterDown = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
loadingBar.setVisibility(View.GONE);
// 파일 다운로드 종료 후 다운받은 파일을 실행시킨다.
showDownloadFile();
}
};
private void showDownloadFile() {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(Save_Path + "/" + File_Name);
// 파일 확장자 별로 mime type 지정해 준다.
if (File_extend.equals("mp3")) {
intent.setDataAndType(Uri.fromFile(file), "audio/*");
} else if (File_extend.equals("mp4")) {
intent.setDataAndType(Uri.fromFile(file), "vidio/*");
} else if (File_extend.equals("jpg") || File_extend.equals("jpeg")
|| File_extend.equals("JPG") || File_extend.equals("gif")
|| File_extend.equals("png") || File_extend.equals("bmp")) {
intent.setDataAndType(Uri.fromFile(file), "image/*");
} else if (File_extend.equals("txt")) {
intent.setDataAndType(Uri.fromFile(file), "text/*");
} else if (File_extend.equals("doc") || File_extend.equals("docx")) {
intent.setDataAndType(Uri.fromFile(file), "application/msword");
} else if (File_extend.equals("xls") || File_extend.equals("xlsx")) {
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.ms-excel");
} else if (File_extend.equals("ppt") || File_extend.equals("pptx")) {
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.ms-powerpoint");
} else if (File_extend.equals("pdf")) {
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
}
startActivity(intent);
}
}
제가참고한사이트입니다
http://motpool.tistory.com/35