-2

I made a downloader and it works but the problem is when I download from mobile has no External storage it doesn't work

here is my code which does well for external storage :-

lateinit var downloadManager :DownloadManager
lateinit var request : DownloadManager.Request
downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

btndownload.setOnClickLisner{
request = DownloadManager.Request(Download_Uri)
request.setAllowedOverRoaming(false)
request.setTitle(songdownloadedname)
request.setDescription(null)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, songdownloadedname)
request.setVisibleInDownloadsUi(true)
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
downloadManager.enqueue(request)

}

What can I edit to download it to internal storage instead of external one ?

  • The answers of this question may help you: https://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – Danny Sep 05 '18 at 02:15
  • so are you getting any error ? – Lucifer Sep 05 '18 at 02:23
  • Possible duplicate of [Android download Audio from server](https://stackoverflow.com/questions/21161736/android-download-audio-from-server) – Hoàng Vũ Anh Sep 05 '18 at 04:27
  • I don't believe you have tried searching before asking, while you have to. – Vladyslav Matviienko Sep 05 '18 at 04:53
  • I used all classes in all answers and it didn't work with me .. so I published the question to get help , shouldn't I ? @VladyslavMatviienko – Mohamed Khaled Sep 05 '18 at 04:56
  • You have to mention what you have tried, or obviously you will be offered the answers you already tried. – Vladyslav Matviienko Sep 05 '18 at 04:58
  • I already did it cause my question is more specific for what I exactly wanna do @VladyslavMatviienko – Mohamed Khaled Sep 05 '18 at 05:02
  • No, you didn't . You didn't show what you tried. – Vladyslav Matviienko Sep 05 '18 at 05:04
  • don't you see your question is already closed (on hold) because it is missing what I am asking you to add? You **really have to** add what you have tried. Please read https://stackoverflow.com/help/how-to-ask before starting talking about somebody's eyes. – Vladyslav Matviienko Sep 05 '18 at 05:09
  • Yeah you are right man .. thanks man maybe I have to be more professional when I ask .. and sorry for any misunderstanding @VladyslavMatviienko – Mohamed Khaled Sep 05 '18 at 05:14
  • No problems. Let me also explain *why you are required to add what you have tried*. If you tried a lot of answers, as you said, it likely means that you did it somehow incorrectly. That's why if you show how exactly you have tried at least something, we will likely find what exactly you did incorrectly. While if you don't show what you tried, you will likely be offered with solutions, you have tried already, and therefore you will just waste time of people who wrote answers. – Vladyslav Matviienko Sep 05 '18 at 05:16
  • I have edited the question Now can you help ?@VladyslavMatviienko – Mohamed Khaled Sep 05 '18 at 08:22

2 Answers2

0

You can use DownloadManager to download file. you can use this blog (not my blog) as reference. Source code below taken from the blog:

public class DownloadManagerActivity extends Activity {
    private long enqueue;
    private DownloadManager dm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {

                            ImageView view = (ImageView) findViewById(R.id.imageView1);
                            String uriString = c
                                    .getString(c
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            view.setImageURI(Uri.parse(uriString));
                        }
                    }
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    public void onClick(View view) {
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Request request = new Request(
                Uri.parse("your mp3 url"));
        enqueue = dm.enqueue(request);

    }

    public void showDownload(View view) {
        Intent i = new Intent();
        i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
        startActivity(i);
    }
}
hakim
  • 3,819
  • 3
  • 21
  • 25
-1

I think you need to check your manifests file AndroidManifests.xml. (You need to allow your app to connect).

try <uses-permission android:name="android.permission.INTERNET" />

ssp
  • 1,666
  • 11
  • 15