将位图显示为null

时间:2016-09-27 08:47:38

标签: java android bitmap

我正在尝试从网址下载图片并将其保存在文件中。但它没有得到拯救。因此,当我调试代码时,我发现位图始终为空。

代码:

  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权限。请帮忙。谢谢..

3 个答案:

答案 0 :(得分: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);
            }
        }

此代码适用于我

答案 1 :(得分: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权限

答案 2 :(得分: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;
    }
}