如何使用端点在app engine上检索图像存储以及如何在Android应用程序中显示
我已经上传了应用程序引擎上的图像。我正在使用端点从应用程序引擎检索android应用程序中的图像blob键。我在android应用程序上做一些代码来显示图像。 代码是如何使用端点在app engine上检索图像存储以及如何在Android应用程序中显示,android,google-app-engine,endpoints,Android,Google App Engine,Endpoints,我已经上传了应用程序引擎上的图像。我正在使用端点从应用程序引擎检索android应用程序中的图像blob键。我在android应用程序上做一些代码来显示图像。 代码是 URL imageURL = null; try { //use our image serve page to get the image URL imageURL = new URL("http://yourapp.appspot.com/serveBlob?id=" + o.getImageKey());
URL imageURL = null;
try
{
//use our image serve page to get the image URL
imageURL = new URL("http://yourapp.appspot.com/serveBlob?id=" + o.getImageKey());
} catch (MalformedURLException e)
{
e.printStackTrace();
}
try {
//Decode and resize the image then set as the icon
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 1 / 2;
Bitmap bitmap = BitmapFactor.decodeStream((InputStream) imageURL.getContent());
Bitmap finImg = Bitmap.createScaledBitmap(bitmap, 50, 50, false);
icon.setImageBitmap(finImg);
} catch (IOException e)
{
e.printStackTrace();
}
但它给了我bitmap=null
并抛出空指针异常
从最近4天开始,我就对这一点印象深刻。请帮帮我。试试这个
Bitmap bitmap = null;
try {
HttpURLConnection connection = (HttpURLConnection) imageURL
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
Log.v("bitmap--", "" + bitmap);
icon.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
我得到了答案
首先必须在我的项目的应用程序引擎端创建Servlet
Servlet是这样的
public class Serve extends HttpServlet
{
private BlobstoreService blobstoreService =BlobstoreServiceFactory.getBlobstoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
BlobKey blobKey = new BlobKey(req.getParameter("id"));
blobstoreService.serve(blobKey, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
doGet(req, resp);
}
}
URL imageURL = new URL("http://xyz.appspot.com/ServeBlob?id="+blobKey);
HttpURLConnection connection = (HttpURLConnection) imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageView img = (ImageView) findViewById(R.id....);
img.setImageBitmap(bitmap);
然后必须在web.xml中注册servlet
<servlet>
<servlet-name>Serve</servlet-name>
<servlet-class>com.xyz.Serve</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Serve</servlet-name>
<url-pattern>/ServeBlob</url-pattern>
</servlet-mapping>
xyz
是appspot上的应用程序引擎项目名称。
blobKey
应该是AppEngine上图像存储的blob键
现在像这样将位图传递到图像视图
public class Serve extends HttpServlet
{
private BlobstoreService blobstoreService =BlobstoreServiceFactory.getBlobstoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
BlobKey blobKey = new BlobKey(req.getParameter("id"));
blobstoreService.serve(blobKey, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
doGet(req, resp);
}
}
URL imageURL = new URL("http://xyz.appspot.com/ServeBlob?id="+blobKey);
HttpURLConnection connection = (HttpURLConnection) imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageView img = (ImageView) findViewById(R.id....);
img.setImageBitmap(bitmap);
如果能用一种更好、更可读的方式对代码进行格式化,那就太好了。是的,Lipis,我已经用一些格式化方法更新了我的问题。可以吗?你能验证用浏览器从你的imageURL下载图像是否有效吗?如果你能成功地将你的图像检索到你的android应用程序中,你可以查看下面的答案,了解我如何理解1rep人可能会弄错格式。。但为什么还要在答案上继续这个趋势呢?!Tamilan,它仍然得到位图--null,它的工作方式和以前的代码相同。