Search code examples
javaandroidandroid-studioarraylistandroid-download-manager

How to loop ArrayList and send each value to Download Manager?


I am trying to send ArrayList(VideoUrlArrayList) values inside a for loop to Android Download Manager but i keep getting following error:

error: incompatible types: String cannot be converted to Uri

and pointing to this line:

DownloadManager.Request request = new DownloadManager.Request(VideoUrlArrayList.get(i));
fileName = VideoUrlArrayList.get(i).substring(VideoUrlArrayList.get(i).lastIndexOf("/"));

if i remove the part of code that sends urls to Download Manager the toast displays the arrayList(VideoUrlArrayList) values correctly !

Could you guys help me fix this problem and successfully send values of my ArrayList to Download Manager?(Furthermore, I want to use the original file names from urls stored in VideoUrlArrayList).Thanks

MainAcitivty.java:

public class MainActivity extends AppCompatActivity {

    private DownloadManager downloadManager;
    private long refid;
    private String fileName;
    ArrayList<Long> list = new ArrayList<>();
    ArrayList<String> VideoUrlArrayList =new ArrayList<String>();


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

        registerReceiver(onComplete,
                new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));



        TextView btnMultiple = (TextView) findViewById(R.id.multiple);


        if(!isStoragePermissionGranted())
        {


        }




btnMultiple.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                list.clear();

                EditText editText1 = (EditText) findViewById(R.id.editText);
                String linesArray[] = editText1.getText().toString().split("\n");



                for (int i = 0; i < linesArray.length; i++) {
                    String currLine = linesArray[i];

                    VideoUrlArrayList.add(currLine);

                }


                for (int i = 0; i < VideoUrlArrayList.size(); i++) {


                    //Toast.makeText(getApplicationContext(), "Array Text: " + VideoUrlArrayList.get(i), Toast.LENGTH_LONG).show();

                    DownloadManager.Request request = new DownloadManager.Request(VideoUrlArrayList.get(i));
                    fileName = VideoUrlArrayList.get(i).substring(VideoUrlArrayList.get(i).lastIndexOf("/"));

                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                    request.setAllowedOverRoaming(false);

                    //Set the title of this download, to be displayed in notifications (if enabled).
                    request.setTitle("Demo Downloading " + fileName);

                    //Set a description of this download, to be displayed in notifications (if enabled)
                    request.setDescription("Downloading " + fileName);

                    //Set whether this download should be displayed in the system's Downloads UI. True by default.
                    request.setVisibleInDownloadsUi(true);

                    //Set the local destination for the downloaded file to a path within the public external storage directory (as returned by getExternalStoragePublicDirectory(String)).
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);

                    refid = downloadManager.enqueue(request);


                    Log.e("OUTNM", "" + refid);

                    list.add(refid);


                }




            }
        });

                   //the rest of code after this

}

Solution

  • Make this line:

    DownloadManager.Request request = new DownloadManager.Request(VideoUrlArrayList.get(i));
    

    to:

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(VideoUrlArrayList.get(i)));
    

    DoesnloadManager.Request() accepts Uri not String. So you need to parse it to Uri.