0

我已经在应用引擎上上传了图片。我正在使用端点从应用引擎中检索 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 天开始,我就对这一点感到震惊。请帮我。

4

2 回答 2

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是您在 apppot 上的应用引擎项目名称。 blobKey应该是 AppEngine 上图像存储的 blob 键。

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

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

img.setImageBitmap(bitmap);
于 2014-01-03T07:07:52.517 回答
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();
            }
于 2013-11-28T11:35:58.103 回答