Java 如何循环ArrayList并将每个值发送到下载管理器?
我正在尝试将for循环中的ArrayListVideoUrlArrayList值发送到Android下载管理器,但我一直收到以下错误:Java 如何循环ArrayList并将每个值发送到下载管理器?,java,android,android-studio,arraylist,android-download-manager,Java,Android,Android Studio,Arraylist,Android Download Manager,我正在尝试将for循环中的ArrayListVideoUrlArrayList值发送到Android下载管理器,但我一直收到以下错误: error: incompatible types: String cannot be converted to Uri 并指向这条线: DownloadManager.Request request = new DownloadManager.Request(VideoUrlArrayList.get(i)); fileName = VideoUrlArra
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("/"));
DownloadManager.Request request = new DownloadManager.Request(VideoUrlArrayList.get(i));
如果我删除将URL发送到下载管理器的代码部分,toast将正确显示arrayListVideoUrlArrayList值
你们能帮我解决这个问题并成功地将ArrayList的值发送到下载管理器吗?此外,我想使用VideoUrlArrayList中存储的URL的原始文件名。谢谢
MainActivity.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));
fileName = VideoUrlArrayList.get(i).substring(VideoUrlArrayList.get(i).lastIndexOf("/"));
DownloadManager.Request request = new DownloadManager.Request(VideoUrlArrayList.get(i));
致:
请求接受Uri而不是字符串。所以您需要将其解析为Uri。谢谢您的回复。我试过了,我得到了这个错误:错误:Uri是抽象的;无法实例化现在我编译时没有收到错误,但当我运行程序并在文本框中输入一些URL并按下按钮时,程序关闭!不幸的是demoApp已停止。即使toast也不显示arrayList数据!这现在是一个不同的问题,您应该为此提供一个代码和logcat,或者将其作为另一个问题提出。请把你的问题缩小到你遇到的具体问题。