Android 从Url下载pdf并将其保存到sd卡
我写了这段代码是为了从Android 从Url下载pdf并将其保存到sd卡,android,download,android-storage,Android,Download,Android Storage,我写了这段代码是为了从url下载pdf,文件url是这样的吗- String fileURL= "http://www.vivekananda.net/PDFBooks/History_of_India.pdf"; 对其进行编码 public static void DownloadFile(String fileURL, File directory) { try { FileOutputStream f = new FileOutputStream(dir
url
下载pdf
,文件url
是这样的吗-
String fileURL= "http://www.vivekananda.net/PDFBooks/History_of_India.pdf";
对其进行编码
public static void DownloadFile(String fileURL, File directory) {
try {
FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.getResponseCode();
c.connect();
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void createPdfFile(){
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
file = new File(folder, "storrage_data.pdf");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}`
public void downloadFile(){
new Thread(new Runnable() {
@Override
public void run() {
Downloader.DownloadFile(url, file);
showPdf();
}
}).start();
}
但这显示了响应代码为405的文件未找到异常。我不知道为什么会发生这种情况。请帮助
这是我在sd卡中创建文件的代码-
对其进行编码
public static void DownloadFile(String fileURL, File directory) {
try {
FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.getResponseCode();
c.connect();
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void createPdfFile(){
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
file = new File(folder, "storrage_data.pdf");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}`
public void downloadFile(){
new Thread(new Runnable() {
@Override
public void run() {
Downloader.DownloadFile(url, file);
showPdf();
}
}).start();
}
在此之后,我从onResume()调用线程中的下载方法,如下所示;因为从onCreate开始,它会给出错误“主线程上的网络”。我现在错在哪里,我不知道:(
对其进行编码
public static void DownloadFile(String fileURL, File directory) {
try {
FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.getResponseCode();
c.connect();
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void createPdfFile(){
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
file = new File(folder, "storrage_data.pdf");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}`
public void downloadFile(){
new Thread(new Runnable() {
@Override
public void run() {
Downloader.DownloadFile(url, file);
showPdf();
}
}).start();
}
您的代码是正确的。现在您需要将pdf文件下载到外部存储器或任何您想要下载和保存它的地方。可能的原因是您想要下载和保存的文件夹不存在。首先检查它是否存在。如果不存在,请创建它。然后创建fileoutputstream并写入它。删除此代码,然后重试
//c.setRequestMethod(“GET”);
//c、 设置输出(真);
//c、 getResponseCode();
//c、 connect();
>我认为<代码> URL .OpenCnutoType()/代码>已经描述了连接,所以<代码> C连接()/代码>不是必要的。
< P>我建议您使用DealBooMistor。在下载过程中会出现太多问题来处理所有这些问题。 下面是我从我的应用程序中提取的一些代码,并对其进行了一些轻微的修改,以消除您不需要的部分public void downloadAndOpenPdf(String url,final File file) {
if(!file.isFile()) {
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
req.setDestinationUri(Uri.fromFile(file));
req.setTitle("Some title");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
unregisterReceiver(this);
if (file.exists()) {
openPdfDocument(file);
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
dm.enqueue(req);
Toast.makeText(this, "Download started", Toast.LENGTH_SHORT).show();
}
else {
openPdfDocument(file);
}
}
public boolean openPdfDocument(File file) {
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file), "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivity(target);
return true;
} catch (ActivityNotFoundException e) {
Toast.makeText(this,"No PDF reader found",Toast.LENGTH_LONG).show();
return false;
}
}
你在“目录”里发送什么参数?我必须在文件参数中给出什么?如果你说的是在sd卡中创建的文件,那么在文件下载之前它将是空的。你能给我完整的代码下载并将pdf保存在sd卡中,然后打开它吗?它是保存下载的pdf的地方。检查它是否不存在,以防用户再次下载相同的文件。在我的应用程序中,URL和文件名之间存在一对一的映射,因此,如果文件存在,则该URL已提前下载。已完成。发生错误,因为pdf URL不正确。!!您的意思是
String fileURL=”http://www.vivekananda.net/PDFBooks/History_of_India.pdf“;
不正确?但是我用这个url做的。。。