2

i have been making an app which download files from internet and save themon storage i have 2 questions :first how to address it to save them in downlod folder which is the exact address? and second where is this "/data/user/0/com.example.moein.download_kotlin/files/Downloads/file.jpg" directory that it is using now? here is my code

            mainFetch = Fetch.Builder(applicationContext, "Download 1 file")
            .setDownloadConcurrentLimit(1) .
            .enableLogging(true)
            .build()

    //Single enqueuing example
    val request = Request("https://www.sample-videos.com/img/Sample-jpg-image-50kb.jpg",
            applicationContext.getFilesDir().getPath().toString() +"/Downloads/file.jpg")
    request.priority=Priority.HIGH;
    request.networkType=NetworkType.ALL
    request.addHeader("download", "2")

    mainFetch!!.enqueue(request, object : Func<Download> {
        override fun call(t: Download) {

        Toast.makeText(applicationContext,t.file,Toast.LENGTH_SHORT).show();
       //toasts /data/user/0/com.example.moein.download_kotlin/files/Downloads/file.jpg

        }
    }, object : Func<Error> {
        override fun call(t: Error) {
            Toast.makeText(applicationContext,t.toString(),Toast.LENGTH_SHORT).show();
        }
    })`
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
Moeinh77
  • 347
  • 5
  • 14

1 Answers1

2

first how to address it to save them in downlod folder which is the exact address?

Use following:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

If you are targetting Andrpid 6.0 and above, remember to request for WRITE_EXTERNAL_STORAGE to write to this directory and READ_EXTERNAL_STORAGE to read from this directory. User needs to grant these permissions,hence you have to implement permission model. You can follow this official tutorial to implement this.

second where is this "/data/user/0/com.example.moein.download_kotlin/files/Downloads/file.jpg" directory that it is using now?

This is your application-specific directory. Files saved to this are private to your app, and other apps cannot access them (nor can the user, unless they have root access). This makes internal storage a good place for internal app data that the user doesn't need to directly access.

However I found on Samsung device that ES file explorer can access this directory. But its a mystery how they manage to do it.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • `This is your application-specific directory which is accessible to your app only.` Indeed. So no other app has access. `You can install any file explorer app like ES file explorer to access and view this directory contents`. Nonsense. ES File Explorer has no access. No other app has access. – greenapps May 13 '18 at 07:05
  • 1
    `Remember to request for WRITE_EXTERNAL_STORAGE to write to this directory and READ_EXTERNAL_STORAGE to read from this directory.`. Indeed. And dont forget for Android 6+ to add code to let the user confirm these permissions at runtime. – greenapps May 13 '18 at 07:07
  • @greenapps you should first try it and comment. Some how ES file explorer can access it and export some contents ( Tested on Samsung Galaxy A5, not rooted) – Sagar May 13 '18 at 07:09
  • @greenapps thank you correcting Android 6.0 part. Updated the answer – Sagar May 13 '18 at 07:10
  • Please send me your A5 ;-) And try other devices! And explain what it means if you say `which is accessible to your app only. `. – greenapps May 13 '18 at 07:12
  • Hahaha.. unfortunately you have to buy it from Samsung store :-P Sure. Let me explain. – Sagar May 13 '18 at 07:14
  • Updated the answer you can help me to check. I will test on more devices to check if ES file explorer works the same. – Sagar May 13 '18 at 07:37
  • ES File Explorer is no good reference as its programmers know some tricks to write to unwritable storage. – greenapps May 13 '18 at 07:38
  • Removed its reference. – Sagar May 13 '18 at 07:45
  • No no ... Everybody should know.. Good info. – greenapps May 13 '18 at 07:45
  • Added the info again – Sagar May 13 '18 at 07:48
  • @sagar hey tnx for answer ,i have a question how can i show the picture that i download with this library and saved it into that address? – Moeinh77 May 14 '18 at 19:04
  • You can maintain (in SharedPreference or local variable) the Uri of the file and then whereever you want to load you can follow this [SO](https://stackoverflow.com/questions/3879992/how-to-get-bitmap-from-an-uri) – Sagar May 15 '18 at 02:10
  • when using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) i get permission denied plz help – Moeinh77 May 18 '18 at 18:52
  • You need to request for READ_EXTERNAL_STORAGE & WRITE_EXTERNAL_STORAGE permission to be able to do that – Sagar May 18 '18 at 23:52