如何循环阵列列表并将每个值发送到下载管理器



我正在尝试将arraylist(vivenourlarrarylist)值发送到for循环到Android下载管理器中

error: incompatible types: String cannot be converted to Uri

指向这一行:

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

如果我删除了发送URL的代码部分以下载Manager,则正确显示arrayList(vivetourLarraryList)值!

你们能帮我解决这个问题,并成功将我的ArrayList值发送到下载管理器吗?

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
}

制作这一行:

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

to:

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

DoesnloadManager.Request()接受URI而不是字符串。因此,您需要将其解析到URI。

最新更新