常用方法(1)------根据图片的url路径获得Bitmap对象

1、 本地url转换为Bitmap

  /**
     * @param uri:图片的本地url地址
     * @return Bitmap;
     */
    private Bitmap decodeUriAsBitmap(Uri uri) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;
    }

2、网络url转换为Bitmap

/**
 * 根据图片的url路径获得Bitmap对象
 * @param url
 * @return
 */
private Bitmap decodeUriAsBitmapFromNet(String url) {
    URL fileUrl = null;
    Bitmap bitmap = null;

    try {
        fileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        HttpURLConnection conn = (HttpURLConnection) fileUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;

}

你可能感兴趣的:(【Android】,【Android常用方法】)