2013-08-28 22 views
0

的Android如何使用Environment.getExternalStorageDirectory()的Android如何使用Environment.getExternalStorageDirectory()corretly

我怎麼能使用Environment.getExternalStorageDirectory()讀取/ SD卡寫一個形象?還是有更好的方法來做到這一點?

我沒有使用源代碼下載電子保存圖片:

try { 

     URL url = new URL(urlBaseImagem + icone); 
     URLConnection conection = url.openConnection(); 
     conection.connect(); 
     // this will be useful so that you can show a tipical 0-100% progress bar 
     int lenghtOfFile = conection.getContentLength(); 

     // 
     // download the file 
     InputStream input = new BufferedInputStream(url.openStream(), 8192); 

     // Output stream     
     //File sdCard = Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE); 
     File sdCard = Environment.getExternalStorageDirectory(); 

     File directory = new File(sdCard.getAbsolutePath() + "/cardapioweb/rest/"); 

     if(directory.exists() == false){ 
      directory.mkdirs(); 
     } 
     File outputFile = new File(directory, icone); 

     OutputStream output = new FileOutputStream(outputFile); 

     byte data[] = new byte[1024]; 

     long total = 0; 

     while ((count = input.read(data)) != -1) { 
      total += count; 
      // publishing the progress.... 
      // After this onProgressUpdate will be called 
      //publishProgress(""+(int)((total*100)/lenghtOfFile)); 

      // writing data to file 
      output.write(data, 0, count); 
     } 

     // flushing output 
     output.flush(); 

     // closing streams 
     output.close(); 
     input.close(); 

     Log.w("CardapioWeb:" , "Imagem " + outputFile.toString() + " salva no sdcard"); 


    } catch (Exception e) { 
     Log.e("Error: ", e.getMessage()); 
    } 

我沒有使用源加載e示出的圖像:

File arq = new File(this.sdCard.getAbsolutePath() + "/cardapioweb/rest/" + filialDataCollection.get(position).get(KEY_ICONE)); 

//verifica se a imagem foi baixada com sucesso 
if(arq.exists() == true){ 
     //deve exibir a imagem do sdcard 
     File outputFile1 = new File(this.sdCard.getAbsolutePath() + "/cardapioweb/rest/", filialDataCollection.get(position).get(KEY_ICONE)); 
     Drawable imagem = Drawable.createFromPath(outputFile1.toString()); 
     holder.tvWeatherImage.setImageDrawable(imagem); 
} 

以上代碼的工作就完全正常模擬器(Android虛擬設備)與Eclipse! 但是當我生成apk並安裝在我的Galaxy S4上時,應用程序無法讀取圖像(不會生成錯誤)。只是不顯示圖像。什麼可能是錯的?

回答

0

放一個記錄在您的代碼,看看有什麼

outputFile1.toString(); 

實際指向的,是你期待什麼?

個人而言,我會用outputFile1.getAbsolutePath();不是下面的行:

Drawable imagem = Drawable.createFromPath(outputFile1.toString()); 
+0

親愛的ThisIsNotMe,我沒有嘗試!仍然沒有在我的銀河S4的作品。非常蠕變! – user2725921

相關問題