2016-04-06 25 views
0

Günlük olarak bir dosyanın güncellenip güncellenmediğini kontrol etmesi gereken bir uygulama yapıyorum. Eğer öyleyse, indirmesi gerekiyordu. Ancak, test ederken, bir çalışma zamanı hatası alırım.Android Studio İndirme Yöneticisi Hata: IllegalArgumentException URI'de beklenen dosya şeması

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:[email protected]/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:[email protected]/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)  

Bu hatayı neden aldığımı anlamıyorum; FTP şeması değil mi?

İşte downloadReceiver sınıfı için kodum. Lütfen dikkat: Şu anda kullanıyorum dosya sadece test için, ben kullanacak gerçek dosya bir .db dosya

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:[email protected]", "/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()); 
     } 
} 

     } 

ise nedense bu yaklaşım (bir URI bir dosya oluşturarak kullanma FTP protokolü ile bir dosya indirmek için DownloadManager), diğer seçeneklerim ne olabilir?

Teşekkürler! Eğer bir URI alır File için yapıcı bakarsak

cevap

1

, okur:

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.

Yani sadece file:// ile başlayan bir URI ile bir File oluşturabilirsiniz.

+0

Teşekkürler! URI'den bir File nesnesi oluşturamadığımdan, en son ne zaman değiştirildiğini kontrol etmenin başka bir yolu var mı? Teşekkürler! – KellyMarchewa

+0

Eğer FTP kullanmanız gerekiyorsa, bir sürü iş yapmalısınız. HTTP veya HTTPS'yi kullanabiliyorsanız HttpURLConnection ve setIfModifiedSince öğelerini kullanabilirsiniz, böylece yalnızca dosya daha yeni ise indirilir. –

+0

Tamam, teşekkürler! – KellyMarchewa

İlgili konular