Java 对从Servlet接收的InputStream进行解码后剪切的图像
我正在从Servlet成功下载png图像,但相应图像的InputStream(存储为位图Bitmap=BitmapFactory.decodeStreamInputStream,null,null)被意外剪切。我的代码:Java 对从Servlet接收的InputStream进行解码后剪切的图像,java,android,http,servlets,bitmap,Java,Android,Http,Servlets,Bitmap,我正在从Servlet成功下载png图像,但相应图像的InputStream(存储为位图Bitmap=BitmapFactory.decodeStreamInputStream,null,null)被意外剪切。我的代码: public class StaggeredPrenotaTour extends ActionBarActivity implements CallBack{ Bitmap mBitmap; ImaveView vImageView; //....
public class StaggeredPrenotaTour extends ActionBarActivity implements CallBack{
Bitmap mBitmap;
ImaveView vImageView;
//....
@Override
protected void onCreate(Bundle savedInstanceState) {
//....
GetXMLTask task = new GetXMLTask(this);
// Execute the task
task.execute(new String[]{URL});
}
//....
public void callbackBitmap(Bitmap bitmap){
mBitmap = bitmap;
//vImageView.setImageBitmap(bitmap);
}
//....
}
GetXMLTask类:
在下面的屏幕截图中,您可以在CardView中看到裁剪后的图像。第二个图像显示第一个的原始大小
为什么会被这样切割?我一直在网上寻思,尝试了一些方法,但直到现在都没有找到解决办法
给你几个问题:1你下载的所有图像都应该是相同大小的吗?2卡片布局的xml文件是什么样的?3为什么要使用BitmapFactory.decodeStream InputStream是,Rect outPadding,BitmapFactory。选项在您的案例中不使用时会选择?1是2此:3我使用了此示例:,但我刚刚删除了位图选项,图像仍然被裁剪…R服务器上的图像大小相同?是的,它们大小相同
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
StaggeredPrenotaTour staggeredPrenotaView;
public GetXMLTask(StaggeredPrenotaTour listView){
this.staggeredPrenotaView = listView;
}
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
staggeredPrenotaView.callbackBitmap(result);
// setimageView.setImageBitmap(result);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
InputStream st = getServletContext().getResourceAsStream("/WEB-INF/imgs/cardbackground9.png");
OutputStream out = resp.getOutputStream();
if (st != null) {
byte[] buf = new byte[4096];
int nRead;
while( (nRead=st.read(buf)) != -1 ) {
out.write(buf, 0, nRead);
}
st.close();
}
out.close();
}
//....