如何使用端点检索应用引擎上的图像存储以及如何在Android应用中显示/显示

时间:2013-11-28 11:13:39

标签: android google-app-engine endpoints

我已在app引擎上传了图片。我正在使用端点从app引擎中检索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());

} 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天开始,我对这一点感到震惊。请帮帮我。

2 个答案:

答案 0 :(得分:0)

试试这个..

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();
            }

答案 1 :(得分:0)

我得到了答案。

首先必须在项目的应用引擎端创建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);
   }

}

然后必须在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>

然后在android代码中使用servlet创建url。

android端的代码是这样的。

 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);

xyz是appspot上的app引擎项目名称。 blobKey应该是AppEngine上图像存储的blob键。

现在将位图传递给图像视图。

ImageView img = (ImageView) findViewById(R.id....);

img.setImageBitmap(bitmap);