tonyofrancis / Fetch

The best file downloader library for Android
https://www.meta.stackoverflow.com/tags/fetch2
Apache License 2.0
1.64k stars 338 forks source link

Not Working #239

Closed lekeope closed 6 years ago

lekeope commented 6 years ago

I tried out the library and it's not working.

Here's my code

class MainActivity : AppCompatActivity() {
// fetchListener code, removed, for the sake of brevity.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val url = "http://www.alexszepietowski.com/wp-content/uploads/downloads/2013/03/The%202%20Golden%20Rules%20of%20Property,%20Business%20and%20Life!.pdf"
        val fileName = "MyFile.pdf"
        val dirPath = "/FileDownloader/PDF/$fileName"

        val fetchConfiguration = FetchConfiguration.Builder(this)
                .setDownloadConcurrentLimit(3)
                .build()

        val fetch = Fetch.getInstance(fetchConfiguration)

        val request = Request(url, dirPath)
        request.priority = Priority.HIGH
        request.networkType =  NetworkType.ALL
        request.addHeader("clientKey", "SD78DF93_3947&MVNGHE1WONG")

        fetch.addListener(fetchListener)

        fetch.enqueue(request)
    }

    fun Log(msg: String) {
        android.util.Log.d("MainActivity", msg)
    }
}

AndroidManifest.xml

<manifest
  ... >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        ...>
        ...
    </application>

</manifest>

Log output D: FetchListener. onError. Error = UNKNOWN_IO_ERROR, Download = DownloadInfo(id=-1721883501, namespace='LibGlobalFetchLib', url='http://www.alexszepietowski.com/wp-content/uploads/downloads/2013/03/The%202%20Golden%20Rules%20of%20Property,%20Business%20and%20Life!.pdf', file='/FileDownloader/PDF/MyFile.pdf', group=0, priority=HIGH, headers={clientKey=SD78DF93_3947&MVNGHE1WONG}, downloaded=0, total=-1, status=FAILED, error=UNKNOWN_IO_ERROR, networkType=ALL, created=1536016053913, tag=null, enqueueAction=REPLACE_EXISTING, identifier=0, downloadOnEnqueue=true, extras={})

you can check out my full code here -> https://github.com/Edge-Developer/FetchDownloadManagerIssue

befora commented 6 years ago

val dirPath = "/FileDownloader/PDF/$fileName" looks incorrect.

Try something like context.getCacheDir() and append your dir path.

lekeope commented 6 years ago

please type out a sample code

befora commented 6 years ago

https://github.com/sethchhim/FetchDownloadManagerIssue/commit/6173636e45b4b9cc61756dd6a61835bd75ac87f3

D/LibGlobalFetchLib: DownloadManager starting download DownloadInfo(id=-94132710, namespace='LibGlobalFetchLib', url='http://www.alexszepietowski.com/wp-content/uploads/downloads/2013/03/The%202%20Golden%20Rules%20of%20Property,%20Business%20and%20Life!.pdf', file='/data/user/0/com.edgedevstudio.fetchdownloadmanagerissue/cache/FileDownloader/PDF/MyFile.pdf', group=0, priority=HIGH, headers={clientKey=SD78DF93_3947&MVNGHE1WONG}, downloaded=0, total=-1, status=QUEUED, error=NONE, networkType=ALL, created=1536090340704, tag=null, enqueueAction=REPLACE_EXISTING, identifier=0, downloadOnEnqueue=true, extras={})
D/LibGlobalFetchLib: FileDownloader starting Download DownloadInfo(id=-94132710, namespace='LibGlobalFetchLib', url='http://www.alexszepietowski.com/wp-content/uploads/downloads/2013/03/The%202%20Golden%20Rules%20of%20Property,%20Business%20and%20Life!.pdf', file='/data/user/0/com.edgedevstudio.fetchdownloadmanagerissue/cache/FileDownloader/PDF/MyFile.pdf', group=0, priority=HIGH, headers={clientKey=SD78DF93_3947&MVNGHE1WONG}, downloaded=0, total=-1, status=QUEUED, error=NONE, networkType=ALL, created=1536090340704, tag=null, enqueueAction=REPLACE_EXISTING, identifier=0, downloadOnEnqueue=true, extras={})
D/MainActivity: FetchListener. onStarted
D/MainActivity: FetchListener. onDownloadBlockUpdated
D/MainActivity: FetchListener. onDownloadBlockUpdated FetchListener. onProgress
D/MainActivity: FetchListener. onCompleted
lekeope commented 6 years ago

i mean, actual code NOT log output

befora commented 6 years ago

The commit is the code, I will paste here for you.

val dirPath = "$cacheDir/FileDownloader/PDF/$fileName"

lekeope commented 6 years ago

thanks man, i'm about to try it out

lekeope commented 6 years ago

hey, it did indicate that file was successfully downloaded but i am unable to find the actual downloaded file.

I've use file explorer and found nothing

befora commented 6 years ago

Good fetch is working.

Cache directory is a sandboxed directory in your application folder. I used this example because sandboxed directories do not require PERMISSIONS to write to it. Just so you know cache directory can be found at /Android/data/.

I rewrote your sample app to request permissions and write to the root DOWNLOAD folder like you are expecting.

https://github.com/sethchhim/FetchDownloadManagerIssue/commit/c083874844ca9f6949f749b2b004eac7f8f7aedf

lekeope commented 6 years ago

Thanks very much! So helpful

lekeope commented 6 years ago

It worked!