我制作了一个下载器,它可以工作,但是问题是,当我从移动设备下载时,没有外部存储设备,它不起作用
这是我的代码,非常适合外部存储:-
lateinit var downloadManager :DownloadManager
lateinit var request : DownloadManager.Request
downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
btndownload.setOnClickLisner{
request = DownloadManager.Request(Download_Uri)
request.setAllowedOverRoaming(false)
request.setTitle(songdownloadedname)
request.setDescription(null)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, songdownloadedname)
request.setVisibleInDownloadsUi(true)
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
downloadManager.enqueue(request)
}
我可以进行哪些编辑以将其下载到内部存储而不是外部存储?
答案 0 :(得分:1)
您可以使用DownloadManager下载文件。您可以将此blog(不是我的博客)用作参考。以下来自博客的源代码:
public class DownloadManagerActivity extends Activity {
private long enqueue;
private DownloadManager dm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
view.setImageURI(Uri.parse(uriString));
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("your mp3 url"));
enqueue = dm.enqueue(request);
}
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}
}
答案 1 :(得分:-1)
我认为您需要检查清单文件AndroidManifests.xml。 (您需要允许您的应用程序连接)。
尝试
<uses-permission android:name="android.permission.INTERNET" />