2015-09-19 2 views
1

J'ai essayé this le code pour le téléchargement de fichier sur GCS dans android. Ce code fonctionne parfaitement et télécharge le fichier sur le compartiment. Maintenant, je veux vérifier les progrès avec la pause, la reprise et la gestion on/off d'Internet. Je recherche beaucoup sur internet mais je n'ai pas trouvé de solution parfaite.Comment montrer le progrès tout en téléchargeant le fichier à GCS dans Android

code

CloudStorage.uploadFile("upload-recording", "/storage/sdcard0/Download/images.jpg",this,file); 

public static void uploadFile(String bucketName, String filePath,Context context,File file_use) 
      throws Exception { 

     this_context = context; 
     this_file = file_use; 
     Storage storage = getStorage(); 

     StorageObject object = new StorageObject(); 
     object.setBucket(bucketName); 

     File file = new File(filePath); 

     InputStream stream = new FileInputStream(file); 
     try { 
      String contentType = URLConnection 
        .guessContentTypeFromStream(stream); 
      InputStreamContent content = new InputStreamContent(contentType, 
        stream); 

      Storage.Objects.Insert insert = storage.objects().insert(
        bucketName, null, content); 
      insert.setName(file.getName()); 

      insert.execute(); 
     } finally { 
      stream.close(); 
     } 
    } 

Répondre

2

Essayez CustomInputStream pour obtenir le statut de lecture des données et afficher à l'aide d'un service.

public class ProgressNotifyingInputStream extends InputStream { 
    private InputStream wrappedStream; 
    private int count = 0; 
    private int totalSize; 
    File file; 
    public ProgressNotifyingInputStream(File file) 
    { 
     this.file=file; 
     } 
    /** 
    * Creates a new notifying outputstream which wraps an existing one. 
    * When you write to this stream the callback will be notified each time when 
    * updateAfterNumberBytes is written. 
    * 
    * @param stream the outputstream to be wrapped 
    * @param totalSize the totalsize that will get written to the stream 
    */ 



    wrappedStream = new FileInputStream(file); 

    public ProgressNotifyingInputStream(InputStream stream, int totalSize) { 
     if(stream==null) { 
      throw new NullPointerException(); 
     } 
     if(totalSize == 0) { 
      throw new IllegalArgumentException("totalSize argument cannot be zero"); 
     } 
     this.wrappedStream = stream; 
     this.totalSize = totalSize; 
    } 


    @Override 
    public int read() throws IOException { 
     count++; 
     return wrappedStream.read(); 
    } 

    /** 
    * Get progress from 0 to 100 
    * @return 
    */ 
    public int getProgress() { 
     return count * 100/totalSize; 
    } 

} 

Et je votre code juste appeler

InputStream stream=new ProgressNotifyingInputStream (file); 
+0

thanyou pour la réponse rapide. En fait j'ai essayé ceci comme \t \t ProgressNotifyingInputStream stream; \t flux = nouveau FileInputStream (fichier); mais il y a une erreur qui est "incompatibilité Type: ne peut pas convertir de FileInputStream à ProgressNotifyingInputStream" s'il vous plaît me guider. – User42590

+0

Vérifiez maintenant :) –

+0

ok merci son travail. mais puis-je reprendre ce téléchargement via ce code? – User42590