2016-09-23 86 views
0

我想從服務器下載圖像並將其轉換爲位圖。我試圖下載圖像並將其轉換爲位圖,但它返回null。我得到位圖爲空。將url轉換爲位圖時獲取位圖

要將圖像轉換爲位圖,我創建了一個asyncTask。

傳遞網址異步任務:

String url = ServiceUrl.getBaseUrl() + ServiceUrl.getImageUserUrl() + profileImage; 
      Log.e("url", url); 


    new ImageUserTask(mContext, url, profileImage).execute(); 

ImageUserAsync任務:

public class ImageUserTask extends AsyncTask<Void, Void, Bitmap> { 
    String imageprofile; 
    private String url; 
    private Bitmap mBitmap; 
    private Context mContext; 

    public ImageUserTask(Context context, String url, String imageprofile) { 
     this.url = url; 
     this.imageprofile = imageprofile; 
     this.mContext = context; 
    } 

    @Override 
    protected Bitmap doInBackground(Void... params) { 
     try { 
      //Url 
      URL urlConnection = new URL(url); 
      //Conntecting httpUrlConnection 
      HttpURLConnection connection = (HttpURLConnection) urlConnection.openConnection(); 
      connection.setDoInput(true); 
      //Connected to server 
      connection.connect(); 
      //downloading image 
      InputStream input = connection.getInputStream(); 
      //converting image to bitmap 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 

      return myBitmap; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

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

     if (result != null) { 


      result = mBitmap; 

      new ImageServer(mContext).save(result); 

     } 
    } 

} 

編輯:

儘量選用畢加索:

 @Override 
    protected void onPostExecute(JSONObject response) { 
     super.onPostExecute(response); 
     count=0; 
     if (response.has("message")) { 
      JSONObject userJson = null; 
      String message = null; 
      count++; 
      try { 

       if (response.getString("message").equalsIgnoreCase(KEY_SUCCESS)) { 
        Toast.makeText(mContext, "user authenticated successfully", Toast.LENGTH_LONG).show(); 
        userJson = response.getJSONObject("user"); 
        String userId = userJson.getString("user_id"); 
        String userName = userJson.getString("user_name"); 
        String profileImage = userJson.getString("profile_image"); 
        String mobileNo = userJson.getString("mobile_no"); 


        String url = ServiceUrl.getBaseUrl() + ServiceUrl.getImageUserUrl() + profileImage; 
        Log.e("url", url); 

        User user = new User(); 

        user.setmUserId(userId); 
        user.setmUserName(userName); 

        user.setmProfileImage(profileImage); 
        user.setmMobileNo(mobileNo); 

        SharedPreferences.Editor editor = mContext.getSharedPreferences("username",mContext.MODE_PRIVATE).edit(); 
        editor.putString("UserUsername",userName); 
        editor.commit(); 

        Target target = new Target() { 
         @Override 
         public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 

          try { 

           File f = new File(mContext.getCacheDir(), "Profile"); 
           f.createNewFile(); 

//Convert bitmap to byte array 
           ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
           bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos); 
           byte[] bitmapdata = bos.toByteArray(); 

//write the bytes in file 
           FileOutputStream fos = new FileOutputStream(f); 
           fos.write(bitmapdata); 
           fos.flush(); 
           fos.close(); 
           Log.e("File",String.valueOf(f)); 
          } 
          catch (IOException e) 
          { 

          } 

         } 

         @Override 
         public void onBitmapFailed(Drawable errorDrawable) { 
         } 

         @Override 
         public void onPrepareLoad(Drawable placeHolderDrawable) { 
         } 
        }; 

         Picasso.with(mContext).load(url).into(target); 


        Toast.makeText(mContext, "user authenticated successfully", Toast.LENGTH_LONG).show(); 


        progressDialog.dismiss(); 
        mContext.startActivity(intent); 
        Picasso.with(mContext).cancelRequest(target); 
        } 
       } 

什麼錯?

+0

發佈您的logcat錯誤? – Nisarg

+0

如果圖像真的存在,你檢查了網址嗎? –

+0

我的第一個想法是記錄傳遞給ImageUserTask的實際URL。 – mm759

回答

-1

首先檢查圖像確實存在與否,如@aman grover所說 (如果可用)使用Picasso Lib下載圖像和URL。

這裏是示例代碼

private Target target = new Target() { 
     @Override 
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
     //Note : here you can get Bitmap 

     } 

     @Override 
     public void onBitmapFailed(Drawable errorDrawable) { 
     } 

     @Override 
     public void onPrepareLoad(Drawable placeHolderDrawable) { 
     } 
} 

private void someMethod() { 
    Picasso.with(this).load("url").into(target); 
} 

@Override 
public void onDestroy() { // could be in onPause or onStop 
    Picasso.with(this).cancelRequest(target); 
    super.onDestroy(); 
} 
+0

用戶沒有要求任何圖書館幫助 –

+0

@RahulKhurana但它更好地使用畢加索然後異步任務並與他,他可以很容易地獲得位圖 – PriyankaChauhan

+0

@RahulKhurana我是對嗎? – PriyankaChauhan

1

試試這個,我用這種方法獲得的圖像。

   URL url1l = new URL("<Image url>"); 

       URLConnection connection = url1l.openConnection(); 
       connection.connect(); 
       // this will be useful so that you can show a typical 0-100% progress bar 
       int fileLength = connection.getContentLength(); 

       // download the file 
       InputStream is = new BufferedInputStream(connection.getInputStream()); 
       bitmap = BitmapFactory.decodeStream(is); 
+0

我試過這個,這也返回null。 @Furqan – Sid

+0

然後檢查圖像是否存在? @Sid – Furqan

+0

我在日誌中得到這個URL http://xesoftwares.co.in/contactsapi/profile_images/d34b638b93773140eb94d5f03c20237c.jpg @Furqan – Sid

0

最有可能的原因是您從服務器收到錯誤或者您收回的數據無法解碼。首先,檢查響應代碼的連接打開後:

connection.connect(); 
int respCode = connection.getResponseCode(); 
Log.d("resp code", "response code " + respCode); 

如果你得到超過200個其他任何東西,那麼什麼是錯與檢索數據(錯誤的URL,身份驗證,或服務器錯誤)。如果確實得到200,那麼問題出在您收到的數據上。將數據讀入一個字節數組並將其轉儲到一個文件中進行檢查。