获取位图为空

问题描述:

我试图从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

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”); –

使用工作,以下方法

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

请试试这个

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