آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

خطا در Download فایل از اینترنت در پروژه(حل شد)

Hossein Kurd  12 سال پیش  11 سال پیش
+1 0

با سلام

من از این کد برای دانلود استفاده می کنم:

 
String url = prdItems.get(0).image_name;
String fileName = url.substring( url.lastIndexOf('/')+1, url.length() );
downloadPath = G.DIR_APP + fileName;

Log.i("Log", "fileName: " + fileName);
Log.i("Log", "URL: " + url);
Log.i("Log", "Local Download Path: " + downloadPath);

FileDownloader fileDownloader = new FileDownloader();
fileDownloader.download(url, downloadPath + "/" + fileName );

FileDownloader:

 

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.util.Log;
import clss.ExceptionHelper;

public class FileDownloader {

public void download(String downloadPath, String filePath){
try {
URL url = new URL(downloadPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();

FileOutputStream outputStream = new FileOutputStream(filePath);
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[32*1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0){
outputStream.write(buffer, 0, len);
}
outputStream.close();

Log.i("Log", "File Downloaded: " + filePath);

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
ExceptionHelper.catchMessage(e);
Log.e("Log","Error Downloading: " + filePath + " From URL: " + downloadPath);
} catch (IOException e) {
// TODO Auto-generated catch block
ExceptionHelper.catchMessage(e);
Log.e("Log","Error Building Connection");
}
}

}

خطای "Error Building Connection" چاپ می شه

0 0
setDoOutput برای ورب post یا put کارایی داره - نه ورب get (12 سال پیش)
+1 0
اون خط رو برداشتم، متاسفانه همون خطا رو می ده! (12 سال پیش)
0 0
ربط به مشکل دانلودت نداشت - منطقا خواسته نابجایی از کانکشنت داری (12 سال پیش)
0 0
باز هم همون خطا رو می ده (12 سال پیش)
0 0
این کد دقیقا کد استاد هست که توی یکی از ویدیو ها آموزش دادن - دسترسی اینترنت و سرور روچک کنید (12 سال پیش)
 برای این سوال 5 پاسخ وجود دارد.
پاسخ به سوال 
Hossein Kurd  12 سال پیش
0 0

آپلود نمونه کد

 

خطا: 

 java.io.FileNotFoundException: /Android/data/Holiday/db/01.jpg/01.jpg: open failed: ENOENT (No such file or directory)

تمامی خطاها:

 08-12 23:05:38.580    2415-2415/com.metago.astro E/eglCodecCommon﹕ writeFully: failed: Broken pipe
08-12 23:05:38.584    2415-2415/com.metago.astro E/gralloc_vbox86﹕ gralloc_lock cacheFlush failed postCount=0 sw_read=1
08-12 23:05:38.584    2415-2415/com.metago.astro E/ViewRootImpl﹕ Could not lock surface
    java.lang.IllegalArgumentException
            at android.view.Surface.nativeLockCanvas(Native Method)
            at android.view.Surface.lockCanvas(Surface.java:452)
            at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2214)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
08-12 23:05:38.904      104-104/? E/BufferQueue﹕ [com.metago.astro/com.metago.astro.SplashActivity] dequeueBuffer: can't dequeue multiple buffers without setting the buffer count
08-12 23:05:38.904    2415-2415/com.metago.astro E/SurfaceTextureClient﹕ dequeueBuffer failed (Invalid argument)
08-12 23:05:38.904    2415-2415/com.metago.astro E/ViewRootImpl﹕ Could not lock surface
    java.lang.IllegalArgumentException
            at android.view.Surface.nativeLockCanvas(Native Method)
            at android.view.Surface.lockCanvas(Surface.java:452)
            at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2214)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
0 0
خطای logcat میگه notFoundException یعنی این url ای که درخواست دانلودش رو دادی وجود نداره! (12 سال پیش)
0 0
خیر - میگه مسیر فایل وجود نداره - 2 بار نام فایل تو مسیرت وجود داره و مشکل همینه (12 سال پیش)
0 0
اهان ، درسته ببخشید ، این روزا تمرکز درست و حسابی ندارم هی گاف پشت گاف ... :)) (12 سال پیش)
پاسخ به سوال 
Hossein Kurd  12 سال پیش
0 0

کلی لاگ نوشتم:

 
// ========== Download Files
String url = prdItems.get(0).image_name;
String fileName = url.substring( url.lastIndexOf('/')+1, url.length() );
downloadPath = G.DIR_APP;

Log.i("Log", "fileName: " + fileName);
Log.i("Log", "URL: '" + url + "'");
Log.i("Log", "Local Download Path: " + downloadPath);

FileDownloader fileDownloader = new FileDownloader();
OnDownloadListener listener = new OnDownloadListener() {

@Override
public void onDownload(int id, int catid, String fileName,
String currentTime) {
// TODO Auto-generated method stub
ImageDetail imageDetail = new ImageDetail();
imageDetail.setDetails(id, catid, fileName, currentTime);
HolidayDB HDB = new HolidayDB();
HDB.insertToTable("Image_Gallery", imageDetail);
Log.i("Log","Imaeg Details Inserted To DataBase On Date: " +
imageDetail.getImage_Date() +
"\n" +
" At Time: " + imageDetail.getImage_Time() +
"\n" +
" With Name: " + imageDetail.getImage_Name() +
"\n" +
" ID: " + imageDetail.getID() +
"\n" +
" CatID: " + imageDetail.getCatID()
);
}
};
fileDownloader.download(url,
downloadPath,
fileName,
prdItems.get(0).id,
prdItems.get(0).catid,
listener);

دانلودر:

 
public class FileDownloader {

@SuppressLint("SimpleDateFormat")
public void download(String urlPath, String filePath, String fileName , int fileID, int fileCatID, OnDownloadListener listener){
G.bldDownloagding = true;
fileName = fileName.replace("/", "");
Log.i("Log", "Directory Path: " + filePath);
Log.i("Log", "File Name: " + fileName);
try {
Log.i("Log", "urlPath is: " + urlPath);
// urlPath = "http://8pic.ir/images/7rhypy9ai9m177xaca7t.png";
Log.i("Log", "Setting Download Path ...");
URL url = new URL(urlPath);
Log.i("Log", "Creating Connection ...");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Log.i("Log", "Connection Created ...");
connection.setRequestMethod("GET");
// connection.setDoOutput(true);
Log.i("Log", "Creating Connection ...");
connection.connect();
Log.i("Log", "Device Connected ...");

long fileSize = connection.getContentLength();
Log.i("Log", "File Size: " + fileSize);

File file = new File(filePath + "/" + fileName);
Log.i("Log", "File Path: '" + filePath + fileName + "'");

if(file.exists()){
long localFileSize = file.length();
// Delete File If Exist
if(fileSize != localFileSize){
file.delete();
}
}
Log.i("Log", "Existing Passed ...");

FileOutputStream outputStream = new FileOutputStream(filePath + "/" + fileName);
Log.i("Log", "OutputStream Created ...");

InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[G.DL_BUFFER_SIZE];
int len = 0;
while ((len = inputStream.read(buffer)) > 0){
outputStream.write(buffer, 0, len);

if(listener != null){
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss a");
String strDate = sdf.format(c.getTime());
listener.onDownload(fileID, fileCatID, fileName, strDate);
}
}
outputStream.close();
inputStream.close();

Log.i("Log", "File Downloaded: " + filePath);

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
ExceptionHelper.catchMessage(e);
Log.e("Log","Error Downloading: " + filePath + " From URL: " + urlPath);
} catch (IOException e) {
// TODO Auto-generated catch block
ExceptionHelper.catchMessage(e);
Log.e("Log","Error Building Connection: " + e);
}
}

}


و لاگ:

 08-13 09:51:19.531: I/Log(12111): Image Galery WebService path is: http://www.holiday-tricot.com/HOL_And/getAllProducts.php
08-13 09:51:19.804: I/LogWebService(12111): getAllProducts.php Json Status Code: 200
08-13 09:51:19.867: I/Log(12111): result: {"posts":[{"id":"1","catid":"9","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/9\/01.jpg"},{"id":"2","catid":"9","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/9\/02.jpg"},{"id":"3","catid":"9","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/9\/03.jpg"},{"id":"4","catid":"9","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/9\/04.jpg"},{"id":"5","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/01.jpg"},{"id":"6","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/02.jpg"},{"id":"7","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/03.jpg"},{"id":"8","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/04.jpg"},{"id":"9","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/05.jpg"},{"id":"10","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/06.jpg"},{"id":"11","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/07.jpg"},{"id":"12","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/08.jpg"},{"id":"13","catid":"8","image_name":"http:\/\/holiday-tricot.com\/HOL_And\/image\/8\/09.jpg"}]}
08-13 09:51:20.023: I/Log(12111): Image >> id: 1
08-13 09:51:20.023: I/Log(12111): Image >> catid: 9
08-13 09:51:20.023: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/9/01.jpg
08-13 09:51:20.023: I/Log(12111): Image >> id: 2
08-13 09:51:20.023: I/Log(12111): Image >> catid: 9
08-13 09:51:20.023: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/9/02.jpg
08-13 09:51:20.023: I/Log(12111): Image >> id: 3
08-13 09:51:20.023: I/Log(12111): Image >> catid: 9
08-13 09:51:20.023: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/9/03.jpg
08-13 09:51:20.023: I/Log(12111): Image >> id: 4
08-13 09:51:20.031: I/Log(12111): Image >> catid: 9
08-13 09:51:20.031: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/9/04.jpg
08-13 09:51:20.031: I/Log(12111): Image >> id: 5
08-13 09:51:20.031: I/Log(12111): Image >> catid: 8
08-13 09:51:20.031: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/01.jpg
08-13 09:51:20.031: I/Log(12111): Image >> id: 6
08-13 09:51:20.031: I/Log(12111): Image >> catid: 8
08-13 09:51:20.031: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/02.jpg
08-13 09:51:20.031: I/Log(12111): Image >> id: 7
08-13 09:51:20.031: I/Log(12111): Image >> catid: 8
08-13 09:51:20.031: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/03.jpg
08-13 09:51:20.031: I/Log(12111): Image >> id: 8
08-13 09:51:20.031: I/Log(12111): Image >> catid: 8
08-13 09:51:20.031: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/04.jpg
08-13 09:51:20.031: I/Log(12111): Image >> id: 9
08-13 09:51:20.031: I/Log(12111): Image >> catid: 8
08-13 09:51:20.031: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/05.jpg
08-13 09:51:20.031: I/Log(12111): Image >> id: 10
08-13 09:51:20.031: I/Log(12111): Image >> catid: 8
08-13 09:51:20.031: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/06.jpg
08-13 09:51:20.031: I/Log(12111): Image >> id: 11
08-13 09:51:20.031: I/Log(12111): Image >> catid: 8
08-13 09:51:20.031: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/07.jpg
08-13 09:51:20.039: I/Log(12111): Image >> id: 12
08-13 09:51:20.039: I/Log(12111): Image >> catid: 8
08-13 09:51:20.039: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/08.jpg
08-13 09:51:20.039: I/Log(12111): Image >> id: 13
08-13 09:51:20.039: I/Log(12111): Image >> catid: 8
08-13 09:51:20.039: I/Log(12111): Image >> image_name: http://holiday-tricot.com/HOL_And/image/8/09.jpg
08-13 09:51:20.039: I/Log(12111): fileName: 01.jpg
08-13 09:51:20.039: I/Log(12111): URL: 'http://holiday-tricot.com/HOL_And/image/9/01.jpg'
08-13 09:51:20.039: I/Log(12111): Local Download Path: /Android/data/Holiday/
08-13 09:51:20.039: I/Log(12111): Directory Path: /Android/data/Holiday/
08-13 09:51:20.039: I/Log(12111): File Name: 01.jpg
08-13 09:51:20.039: I/Log(12111): urlPath is: http://holiday-tricot.com/HOL_And/image/9/01.jpg
08-13 09:51:20.039: I/Log(12111): Setting Download Path ...
08-13 09:51:20.046: I/Log(12111): Creating Connection ...
08-13 09:51:20.046: I/Log(12111): Connection Created ...
08-13 09:51:20.046: I/Log(12111): Creating Connection ...
08-13 09:51:20.085: I/Log(12111): Device Connected ...
08-13 09:51:20.179: I/Log(12111): File Size: 20470
08-13 09:51:20.179: I/Log(12111): File Path: '/Android/data/Holiday/01.jpg'
08-13 09:51:20.179: I/Log(12111): Existing Passed ...
08-13 09:51:20.203: E/Log(12111): Error Building Connection: java.io.FileNotFoundException: /Android/data/Holiday/01.jpg: open failed: ENOENT (No such file or directory)

0 0
من باید توی FileOutputStream تعیین کنم که فایل خروجی در چه مسیری قرار می گیره، اون مسیر هم روی SDCard هست (12 سال پیش)
0 0
mkdirs تمامی پوشه های ساخته نشده تو مسیر فایلتو میسازه - احتمالا رعایت نشده (12 سال پیش)
0 0
چک می کنم توی SDCard ساخته شده (12 سال پیش)
0 0
ضمنا مسیری که میخوای توش فایل رایت کنی باید حاوی نام پکیج هم باشه - این ریزه کاریا رو بقیه سخت تر میتونن تو کدت ببینن - باید بگی داری چیکار میکنی (12 سال پیش)
پاسخ به سوال 
Hossein Kurd  12 سال پیش
0 0

من این رو زدم:

                        String strTarget = filePath + fileName;
new File(strTarget).mkdirs();
Log.i("Log", "strTarget: " + strTarget);
پاسخ به سوال 
Hossein Kurd  12 سال پیش
0 0

خب این هم با مسیری که نام پکیج هم توش هست:

08-13 11:07:45.593: I/Log(18970): fileName: 01.jpg
08-13 11:07:45.601: I/Log(18970): URL: 'http://holiday-tricot.com/HOL_And/image/9/01.jpg'
08-13 11:07:45.601: I/Log(18970): Local Download Path: /data/data/com.emaarit.holiday/files/Holiday/
08-13 11:07:45.601: I/Log(18970): Directory Path: /data/data/com.emaarit.holiday/files/Holiday/
08-13 11:07:45.601: I/Log(18970): File Name: 01.jpg
08-13 11:07:45.601: I/Log(18970): urlPath is: http://holiday-tricot.com/HOL_And/image/9/01.jpg
08-13 11:07:45.601: I/Log(18970): Setting Download Path ...
08-13 11:07:45.601: I/Log(18970): Creating Connection ...
08-13 11:07:45.609: I/Log(18970): Connection Created ...
08-13 11:07:45.609: I/Log(18970): Creating Connection ...
08-13 11:07:45.765: I/Log(18970): Device Connected ...
08-13 11:07:45.867: I/Log(18970): File Size: 20470
08-13 11:07:45.867: I/Log(18970): File Path: '/data/data/com.emaarit.holiday/files/Holiday/01.jpg'
08-13 11:07:45.867: I/Log(18970): Existing Passed ...
08-13 11:07:45.906: E/Log(18970): Error Building Connection: java.io.FileNotFoundException: /data/data/com.emaarit.holiday/files/Holiday: open failed: EISDIR (Is a directory)

این رو ببینید:

 Directory Path: /data/data/com.emaarit.holiday/files/Holiday/

با این کد ساخته شده:

 		DIR_APP = context.getFilesDir() + "/Holiday/";
DIR_DATABASE = DIR_APP + "db/";
new File(DIR_APP).mkdirs();
new File(DIR_DATABASE).mkdirs();

می خوام توی این مسیر فایل رو دانلود کنه

0 0
حل شد (12 سال پیش)
پاسخ به سوال 
سپهر  11 سال پیش
0 0

 من یک مشکلی در دریافت حجم فایل دارم زمانی که از این کد 

 connection.getContentLength()

برای گرفتن حجم فایل استفاده میکنم به من -1 برمی گردونه به نظر شما مشکلش چیه؟

0 0
ممکنه آدرس فایلو پیدا نمیکنه تا حجم واقعی رو برگردونه برای همین -1 برمیگردونه. (11 سال پیش)
0 0
نه فایل موجوده اما حجمش 6 7 کیلوبایته، از برخی از سرورا که فایل میذارم واسه دانلود این مشکل پیش میاد وقتی حجمشون کمه (11 سال پیش)
0 0
متغیری که حجمو نشون میده از نوع int هست؟اگر آره از نوع float بگیرین. (11 سال پیش)
+2 0
از طریق این کد مشکلم حل شد connection.setRequestProperty("Accept-Encoding", "identity"); (11 سال پیش)

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .