2012-02-17 168 views
4

我嘗試下載的圖像與此代碼並保存: 在這一行:無法保存圖片到SD卡

OutputStream os =new FileOutputStream(f); 

我嘗試它的emulator.my應用得到不同的是沒有權限保存到該路徑:

java.io.FileNotFoundException: /mnt/sdcard/InterFlora/1221618532: open failed: EACCES (Permission denied) 

我也加入這一行:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

這是代碼:

RetreiveImage extends AsyncTask<String, Void,Bitmap> { 
      Item m_item; 
      File f; 

      public RetreiveImage(Item aItem , File aFile){ 
       m_item = aItem; 
       f = File (m_Sdcard.getAbsolutePath() + "/myFolder"); 
      } 

     @Override 
     protected Bitmap doInBackground(String... params) { 

     try 
     { 
      Bitmap bitmap=null; 
      InputStream is=new URL(m_item.small).openStream(); 
      OutputStream os =new FileOutputStream(f); 
      os.close(); 

      return bitmap; 
     } 
     catch (MalformedURLException e) { 
      return null; 
     } catch (IOException e) { 
      return null; 
     } 
    } 
} 

編輯

File m_Sdcard = Environment.getExternalStorageDirectory(); 
File cacheDir =new File (m_Sdcard.getAbsolutePath() + "/MyFolder"); 
if(!cacheDir.exists()) 
cacheDir.mkdirs(); 
+0

你應該添加android.permission.WRITE_EXTERNAL_STORAGE權限清單文件 – 2012-02-17 19:25:16

+0

我加入這行已經 – MTA 2012-02-17 19:32:08

+0

顯示你的清單,並在那裏你加入這行 – 2012-02-17 19:35:02

回答

0
Try this works.. 

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import android.app.Activity; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
    import android.net.Uri; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 

    public class main extends Activity { 
    /** Called when the activity is first created. */ 
    public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
    private Button startBtn; 
    private ProgressDialog mProgressDialog; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     startBtn = (Button)findViewById(R.id.startBtn); 
     startBtn.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) { 
       startDownload(); 
      } 
     }); 
    } 

    private void startDownload() { 
     String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg"; 
     new DownloadFileAsync().execute(url); 

    } 
    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DIALOG_DOWNLOAD_PROGRESS: 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Downloading file.."); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
      return mProgressDialog; 
     default: 
      return null; 
     } 
    } 

class DownloadFileAsync extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 

    @Override 
    protected String doInBackground(String... aurl) { 
     int count; 

    try { 

    URL url = new URL(aurl[0]); 
    URLConnection conexion = url.openConnection(); 
    conexion.connect(); 

    int lenghtOfFile = conexion.getContentLength(); 
    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
    FileOutputStream obj; 
    InputStream input = new BufferedInputStream(url.openStream()); 
    //obj=openFileOutput("some_photo_from_gdansk_poland.jpg", Context.MODE_PRIVATE); 
    String Path="/sdcard/";//"/data/data/com.server/"; 

    OutputStream output = new FileOutputStream(Path+"some_photo_from_gdansk_poland.jpg");//obj 

    byte data[] = new byte[1024]; 

    long total = 0; 

     while ((count = input.read(data)) != -1) { 
      total += count; 
      publishProgress(""+(int)((total*100)/lenghtOfFile)); 
      output.write(data, 0, count); 
     } 

     output.flush(); 
     output.close(); 
     input.close(); 
    } catch (Exception e) {} 
    return null; 

    } 
    protected void onProgressUpdate(String... progress) { 
     Log.d("ANDRO_ASYNC",progress[0]); 
     mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    @Override 
    protected void onPostExecute(String unused) { 
     dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 
} 

    } 

    and add android.permission.WRITE_EXTERNAL_STORAGE 
0

你的方式來檢測外部存儲位置比Munish卡普爾的答案更好,因爲/ SD卡並不總是正確的文件夾路徑。

但之後,你確定/ myfolder作爲文件夾存在嗎?您應該創建一個指向該位置的文件,並在其上調用mkdirs來創建它。

之後,保存你的文件。

+0

我創建它之前,我編輯我的帖子 – MTA 2012-02-17 21:02:15

+0

我剛剛檢查,並在創建存在檢查後的目錄後,再次嘗試此代碼存在檢查告訴該目錄不再存在 – MTA 2012-02-17 21:04:52

+0

你可能已經搞砸了你的Linux文件系統在你的android上。它發生了一些時間。取出你的卡,刪除文件夾/ mnt/sdcard,然後重新啓動,把卡放回。你應該看到你的SD卡的內容,再試一次你的代碼。 – Snicolas 2012-02-17 22:11:41

1

嘗試使用此方法得到的文件夾(與創建它,如果它不存在),然後使用F文件var當成你在你的代碼做:

File cacheDirectory; 

    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
     cacheDirectory = new File(android.os.Environment.getExternalStorageDirectory(), "myFolder/"); 
    else 
     cacheDirectory = this.getCacheDir(); 

    if(!cacheDirectory.exists()) 
     cacheDirectory.mkdirs(); 

    File f = new File(cacheDirectory, "my_file.jpg"); 

閱讀鏈接getCacheDir()http://developer.android.com/reference/android/content/Context.html#getCacheDir%28%29

+0

我在這裏得到NULLPOINTER錯誤:android.os.Environment.getExternalStorageDirectory()..任何想法,爲什麼它來..如何可以解決這個問題。 – CoDe 2013-02-06 19:41:25

4

將此行添加到manifest.xml工作!

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+1

確認這與READ權限一起工作 – AlexVPerl 2014-02-18 00:12:23