我正在构建一个应用程序,它应该每天检查文件是否已更新。如果是这样,它应该下载它。但是,在测试时,我收到了运行时错误。
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协议下载文件),我的其他选择可能是什么?
谢谢!
答案 0 :(得分:1)
如果你看一下带有URI的File
的构造函数,它会显示:
使用指定URI的路径构造新文件。
uri
需要是一个绝对的,分层的统一资源标识符 with file scheme 和非空路径组件,但具有未定义的权限,查询或片段组件。
因此,您只能使用以File
开头的URI创建file://
。