我想从服务器下载图像并将其转换为位图。我尝试下载图像并将其转换为位图,但它返回null。我将位图视为空。
要将图像转换为位图,我创建了一个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 :(得分: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);
答案 1 :(得分:0)
最可能的原因是您从服务器收到错误,或者您无法解码您收到的数据。首先,在打开连接后检查响应代码:
connection.connect();
int respCode = connection.getResponseCode();
Log.d("resp code", "response code " + respCode);
如果你得到的不是200,那么检索数据有问题(错误的网址,身份验证或服务器错误)。如果你得到200,那么问题在于你收到的数据。将数据读入字节数组并将其转储到文件中进行检查。
答案 2 :(得分:-1)
首先检查图像确实存在或不存在,因为@aman grover说 如果可以,请使用Picasso Lib下载图片并从网址中下载。
这是示例代码
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();
}