如何循环 ArrayList 并将每个值发送到下载管理器?
How to loop ArrayList and send each value to Download Manager?
我正在尝试将 for 循环内的 ArrayList(VideoUrlArrayList) 值发送到 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 发送到下载管理器的代码部分,toast 会正确显示 arrayList(VideoUrlArrayList) 值!
你们能帮我解决这个问题并成功将我的 ArrayList 的值发送到下载管理器吗?(此外,我想使用存储在 VideoUrlArrayList 中的 url 中的原始文件名)。谢谢
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));
至:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(VideoUrlArrayList.get(i)));
DoesnloadManager.Request()
接受 Uri 而不是字符串。所以需要解析成Uri。
我正在尝试将 for 循环内的 ArrayList(VideoUrlArrayList) 值发送到 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 发送到下载管理器的代码部分,toast 会正确显示 arrayList(VideoUrlArrayList) 值!
你们能帮我解决这个问题并成功将我的 ArrayList 的值发送到下载管理器吗?(此外,我想使用存储在 VideoUrlArrayList 中的 url 中的原始文件名)。谢谢
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));
至:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(VideoUrlArrayList.get(i)));
DoesnloadManager.Request()
接受 Uri 而不是字符串。所以需要解析成Uri。