提取:如何下载文件?

时间:2018-09-17 08:49:10

标签: java android

我正在尝试为我的Android应用使用Fetch Library从服务器下载文件。

我正在使用以下代码,该代码是从github提取示例中复制的:

public class MainActivity extends AppCompatActivity {
    private Fetch fetch;
    Request request;
    Button download;
    String url = "https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
    String file = "/Download/image.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        download = findViewById(R.id.button);
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(MainActivity.this)
                        .setDownloadConcurrentLimit(3)
                        .build();
                fetch= Fetch.Impl.getInstance(fetchConfiguration);
                request = new Request(url,file);
                request.setPriority(Priority.HIGH);
                request.setNetworkType(NetworkType.ALL);
                fetch.enqueue(request,updatedRequest -> {
                    //something
                },error -> {
                    //somethig
                });
            }
        });
    }
} 

但是当我单击下载按钮时,这是logcat向我显示的错误:

    09-17 14:07:36.522 27133-27133/rahul.gaur.test1 E/Process: android_os_Process_getProcessNameByPid pid is 27133
    android_os_Process_getProcessNameByPid value is ahul.gaur.test1
09-17 14:07:43.012 27133-27365/rahul.gaur.test1 E/LibGlobalFetchLib: FileDownloader download:DownloadInfo(id=1284541380, 

namespace='LibGlobalFetchLib', url='https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 

file='/Download/image.png', group=0, priority=HIGH, headers={}, downloaded=0, total=5969, status=QUEUED, 

error=NONE, networkType=ALL, created=1537173461169, tag=null, enqueueAction=REPLACE_EXISTING, identifier=0, 

downloadOnEnqueue=true, extras={})
    java.io.FileNotFoundException: /Download/image.png: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:456)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:117)
        at com.tonyodev.fetch2.downloader.SequentialFileDownloaderImpl$run$1.<init>(SequentialFileDownloaderImpl.kt:92)
        at com.tonyodev.fetch2.downloader.SequentialFileDownloaderImpl.run(SequentialFileDownloaderImpl.kt:90)
        at com.tonyodev.fetch2.downloader.DownloadManagerImpl$start$$inlined$synchronized$lambda$1.run

(DownloadManagerImpl.kt:97)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1115)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:590)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
        at libcore.io.Posix.open(Native Method)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
        at libcore.io.IoBridge.open(IoBridge.java:442)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:117) 
        at com.tonyodev.fetch2.downloader.SequentialFileDownloaderImpl$run$1.<init>(SequentialFileDownloaderImpl.kt:92) 
        at com.tonyodev.fetch2.downloader.SequentialFileDownloaderImpl.run(SequentialFileDownloaderImpl.kt:90) 
        at com.tonyodev.fetch2.downloader.DownloadManagerImpl$start$$inlined$synchronized$lambda$1.run

(DownloadManagerImpl.kt:97) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1115) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:590) 
        at java.lang.Thread.run(Thread.java:818) 

我的Graddle文件:应用插件:“ com.android.application”

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "rahul.gaur.test1"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName 'Alpha'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '28.0.2'
    compileOptions {
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.tonyodev.fetch2:fetch2:2.2.0-RC12'
    //implementation 'com.writingminds:FFmpegAndroid:0.3.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 

我无法下载图片。帮我弄清楚如何轻松地使用它。

在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

字符串文件=“ /Download/image.png”;

使用而不是上面的代码

字符串文件=“ /downloads/image.png”;

或用作示例

字符串url =“ http:www.example.com/test.txt”;

字符串文件=“ /downloads/test.txt”;

在build.gradle中

尝试

实现“ com.tonyodev.fetch2okhttp:fetch2okhttp:2.2.0-RC12”

代替

实现'com.tonyodev.fetch2:fetch2:2.2.0-RC12'

答案 1 :(得分:0)

所以最终我找到了答案,这是非常糟糕的事情。但这就是它:

现在我正在使用的文件字符串是:/Download/image.png

引起错误的

现在我将字符串用作:EEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/fetch/image.png";

它正在正常工作。

很抱歉造成了麻烦,感谢您尝试帮助我@Mohammod