2016-09-27 46 views
0

我試圖從url下載圖像並將其保存在文件中。但它沒有得到保存。所以當我調試代碼時,我發現位圖總是空的。獲取位圖爲空

代碼:

public class ImageUserTask extends AsyncTask<Void, Void,String> { 
    String strURL, imageprofile; 
    Bitmap mBitmap = null; 
    Context mContext; 
    private File profileFile; 

    public ImageUserTask(Context context, String url) { 
     this.strURL = url; 
     this.imageprofile = imageprofile; 
     this.mContext = context; 
    } 
    @Override 
    protected String doInBackground(Void... params) { 

     Bitmap bitmap = null; 
     File directory = null; 
     try { 

      URL url = new URL(strURL); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      // InputStream input = connection.getInputStream(); 
      bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); //This bitmap is null always 

      directory = Environment.getExternalStorageDirectory(); 

      // Create a new folder in SD Card 
      File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/Profile"); 

      if (!directory.exists() && !directory.isDirectory()) { 
       directory.mkdirs(); 
      } 

      File mypath = new File(dir,"ProfileImage"); 
      saveFile(mypath, bitmap); 

     } catch (MalformedURLException e) { 

     } catch (IOException e) { 

     } 
     return directory.getAbsolutePath(); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     if (result != null) { 

      imageprofile = result; 

     } 
    } 

    private void saveFile(File fileName, Bitmap bmp) { 

     FileOutputStream outputStream = null; 

     try { 

      outputStream = new FileOutputStream(fileName); 
      bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); // 100 will be ignored 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (outputStream != null) { 
        outputStream.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

編輯:

public class ImageUserTask extends AsyncTask<Void,Void,Bitmap> { 
    String strURL, imageprofile; 
    Bitmap mBitmap = null; 
    Context mContext; 
    private File profileFile; 

    public ImageUserTask(Context context, String url) { 
     this.strURL = url; 
     this.imageprofile = imageprofile; 
     this.mContext = context; 
    } 
    @Override 
    protected Bitmap doInBackground(Void... params) { 

     getImageFromUrl(strURL); 

     return mBitmap; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     super.onPostExecute(result); 

     if (result != null) { 

      Bitmap bitmap = result; 

     } 
    } 
    public Bitmap getImageFromUrl(String urlString) { 
     try { 
      URL url = new URL(urlString); 
      try { 
       if(mBitmap!=null) { 
        mBitmap.recycle(); 
        mBitmap=null; 
       } 
       HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
       connection.setDoInput(true); 
       //Connected to server 
       connection.connect(); 
       //downloading image 
       InputStream input = connection.getInputStream(); 
       mBitmap = BitmapFactory.decodeStream(input); 

       convertBitmapToFile(mBitmap, urlString); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     return mBitmap; 
    } 

    public File convertBitmapToFile(Bitmap bitmap, String fileName) { 
     ContextWrapper cw = new ContextWrapper(mContext); 
     File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); 
     File mypath = new File(directory, fileName); 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(mypath); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      fos.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return mypath; 

    } 
} 

什麼能是什麼原因?我也添加了Internet權限。請幫忙。謝謝。

+1

張貼您的logcat日誌 – Nikhil

+0

您是否添加了訪問sd卡的權限? – Payal

+0

沒有得到任何異常。 @indramurari – Sid

回答

0
class DownloadFile extends AsyncTask<String, Integer, String> { 
       String strFolderName; 
       String shareType; 
       String downloadPath = ""; 
       Activity mContext; 
      public DownloadFile(Activity mContext) { 
       this.mContext = mContext; 
      } 

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

      @Override 
      protected String doInBackground(String... aurl) { 
       int count; 
       try { 
        String fileName = "your filename with ext"; 
        Log.d("TAG", fileName); 
        URL url = new URL("your url"); 
        URLConnection conexion = url.openConnection(); 
        conexion.connect(); 

        String PATH = "your Path you want to store" + "/"; 
        downloadPath = PATH + fileName; 
        InputStream input = new BufferedInputStream(url.openStream()); 
        OutputStream output = new FileOutputStream(downloadPath); 
        byte data[] = new byte[1024]; 
        while ((count = input.read(data)) != -1) { 
         output.write(data, 0, count); 
        } 
        output.flush(); 
        output.close(); 
        input.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       return null; 
      } 


      protected void onPostExecute(String path) { 
       super.onPostExecute(path); 
      } 
     } 

此代碼對我來說是

+0

什麼是shareType?從哪裏傳遞url? @Mohit Trivedi – Sid

+0

我更改了刪除shareType的代碼並將您的URL放入此語句中URL url = new URL(「your url」); –

0

使用工作,以下方法

public Bitmap getImageFromUrl(String urlString) { 
       Bitmap bmp = null; 
       try { 
        URL url = new URL(urlString); 
        try { 
         if(bmp!=null) { 
          bmp.recycle(); 
          bmp=null; 
         } 
         bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
         convertBitmapToFile(bmp, urlString); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 
       return bmp; 
      } 

      public File convertBitmapToFile(Bitmap bitmap, String fileName) { 
       ContextWrapper cw = new ContextWrapper(activityRef.getApplicationContext()); 
       File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); 
           File mypath = new File(directory, fileName); 
       FileOutputStream fos = null; 
       try { 
        fos = new FileOutputStream(mypath); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
        fos.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       return mypath; 

      } 

添加Android權限的網絡和存儲

+0

我仍然將位圖變爲空。 @ n9153 – Sid

+0

請在瀏覽器中打你的網址。你是否得到你的形象? – Nishith

+0

是的,我確實擁有它。 @ n9153 – Sid

0

請試試這個

public static Bitmap getBitmapFromURL(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 
+0

我得到一個IOException。 @ n9153 – Sid