gpt4 book ai didi

java - Android Studio 下载管理器错误 : IllegalArgumentException Expected file scheme in URI

转载 作者:行者123 更新时间:2023-12-01 10:06:05 25 4
gpt4 key购买 nike

我正在构建一个应用程序,该应用程序应该每天检查文件是否已更新。如果是的话,应该下载它。但是,在测试时,我收到一个运行时错误。

04-06 15:05:51.868 9275-9275/midamcorp.com.burgerkingapp E/AndroidRuntime: FATAL EXCEPTION: main                                                                           java.lang.RuntimeException: Unable to start receiver midamcorp.com.burgerkingapp.downloadReceiver: java.lang.IllegalArgumentException: Expected file scheme in URI: ftp://myusername:mypassword@ftp.shiningbyte.com/public_html/img/fish.png
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2130)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4429)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:3151)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:2918)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Expected file scheme in URI: ftp://myusername:mypassword@ftp.shiningbyte.com/public_html/img/fish.png
at java.io.File.checkURI(File.java:225)
at java.io.File.<init>(File.java:177)
at midamcorp.com.burgerkingapp.downloadReceiver.onReceive(downloadReceiver.java:32)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2123)
at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4429) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:3151) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:2918) 
at dalvik.system.NativeStart.main(Native Method) 

我不明白为什么会收到此错误; FTP不是这个方案吗?

这是我的 downloadReceiver 类的代码。请注意:我当前使用的文件仅用于测试,我将使用的实际文件是 .db 文件

package midamcorp.com.burgerkingapp;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

public class downloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent i) {
File localDBFile = new File(c.getFilesDir(), "bk.db");
File remoteDB = null;
try {

URI uri = new URI("ftp", "myusername:mypassword@ftp.shiningbyte.com", "/public_html/img/fish.png", null, null);
remoteDB = new File(uri);

if(remoteDB.exists()) {
// if(remoteDB.lastModified() > localDBFile.lastModified()) { // the database has been updated; proceed with download

DownloadManager manager = (DownloadManager) c.getSystemService(c.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.fromFile(remoteDB));
request.setDestinationUri(Uri.fromFile(c.getFilesDir()));
// if (localDBFile.exists()) {
// localDBFile.delete();
// }

manager.enqueue(request);
Toast.makeText(c, "Downloaded to " + c.getFilesDir().toString(), Toast.LENGTH_LONG);
// }


}
} catch(URISyntaxException ex) {
Log.e("URI Error", ex.getMessage());
}
}

}

如果由于某种原因采用这种方法(从 URI 创建文件,使用 DownloadManager 通过 FTP 协议(protocol)下载文件),我的其他选择可能是什么?

谢谢!

最佳答案

如果您查看采用 URI 的 File 构造函数,它会显示:

Constructs a new File using the path of the specified URI. uri needs to be an absolute and hierarchical Unified Resource Identifier with file scheme and non-empty path component, but with undefined authority, query or fragment components.

因此,您只能创建一个具有以 file:// 开头的 URI 的 File

关于java - Android Studio 下载管理器错误 : IllegalArgumentException Expected file scheme in URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36461743/

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