我已成功从我的Servlet下载png
图像,但相应图像的InputStream(存储为带Bitmap bitmap = BitmapFactory.decodeStream(InputStream, null, null)
的位图)会被无法预测地裁剪。我的代码:
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
上课:
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;
}
}
Servlet
的{{1}}方法:
doGet
在以下屏幕截图中,您可以在@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();
}
//....
中看到生成的裁剪图像。第二张图显示了第一张图片的原始大小。
为什么会这样切割?我一直在网上思考并尝试了几件事,但直到现在才找到解决办法。