2010-04-27 6 views
1

J'avais une partie du code qui supposait obtenir une image du site web et la stocker dans la carte SD. Le code suivant travaillait trouver quand je développe sur sdk1.5. Cependant, il ne fonctionnait pas maintenant après que je l'ai changé en android sdk 2.0. Cette ligne a eu un problème; FileOutputStream fos = nouveau FileOutputStream (filepath + "/" + this.filename);Erreur FileOutputStream

Voici le code que j'ai:

void downloadFile(String fileUrl) { 
    URL myFileUrl = null; 
    try { 
     myFileUrl = new URL(fileUrl); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     HttpURLConnection conn = (HttpURLConnection) myFileUrl 
       .openConnection(); 
     conn.setDoInput(true); 
     conn.connect(); 

     InputStream is = conn.getInputStream(); 

     bmImg = BitmapFactory.decodeStream(is); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     String filepath = Environment.getExternalStorageDirectory() 
       .getAbsolutePath(); 
     FileOutputStream fos = new FileOutputStream(filepath + "/" 
       + this.filename); 
     bmImg.compress(CompressFormat.JPEG, 75, fos); 
     fos.flush(); 
     fos.close(); 

     Context context = this.getBaseContext(); 
     new MediaScannerNotifier2(context, filepath + "/" + this.filename, 
       "image/jpeg"); 

     // displaying download completion message 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Wallpaper Downloaded").setCancelable(false) 
       .setPositiveButton("ok", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int id) { 
           dialog.cancel(); 
           btn_setwall.setEnabled(true); 
           btn_download.setEnabled(false); 
          } 
         }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } catch (Exception e) { 
     Log.e("MyLog", e.toString()); 
    } 

} 

L'erreur se produit dans la 3ème prise. Cependant, lorsque je déplace cette ligne

FileOutputStream fos = new FileOutputStream (filepath + "/" + this.filename);

au 2ème essai/attraper, alors il se produira dans la 2ème prise. Pouvez-vous m'aider à ce sujet?

+0

"L'erreur" - quelle erreur? Qu'est-ce qui ne va pas? –

+0

Je ne trouve pas l'image dans ma carte SD et je ne vois aucun message d'erreur dans logcat – Lynnooi

+0

Je viens de recevoir un message d'erreur de la capture indiquant "java.io.FileNotFoundException:/sdcard /" – Lynnooi

Répondre

0

Peut-être essayer de se débarrasser de .getAbsolutePath()

Cela fonctionne pour moi 2.2:

FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + fileName); 
Questions connexes