gpt4 book ai didi

java - 如何循环ArrayList并将每个值发送到下载管理器?

转载 作者:行者123 更新时间:2023-12-02 11:51:59 25 4
gpt4 key购买 nike

我试图将 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中的原始文件名)。谢谢

MainAcitivity.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。

关于java - 如何循环ArrayList并将每个值发送到下载管理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47825805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com