0

谁能告诉我为什么没有创建位图?如果我做错了什么,请检查其他代码。

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("URL");

    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(new BasicNameValuePair("username",username));
    UrlEncodedFormEntity urlencodedformentity = new UrlEncodedFormEntity(list);

    httppost.setEntity(urlencodedformentity);
    HttpResponse response = httpclient.execute(httppost);
    InputStream is = response.getEntity().getContent();

// following Bitmap is getting null

    Bitmap bm = BitmapFactory.decodeStream(new BufferedInputStream(is));
    if(bm==null)
    {
        Toast.makeText(this,"Bitmap null", Toast.LENGTH_LONG).show();
    }
    else
    {
        img.setImageBitmap(bm);
    }

客户端将图像发送到服务器

Bitmap b = ((BitmapDrawable) img.getDrawable()).getBitmap();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(CompressFormat.JPEG, 100, baos);

byte ar[] = baos.toByteArray();
String s = Base64.encodeToString(ar, 0);

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("URL");

List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("username",username));
list.add(new BasicNameValuePair("image",s));
UrlEncodedFormEntity urlencodedformentity = new UrlEncodedFormEntity(list);

httppost.setEntity(urlencodedformentity);
HttpResponse response = httpclient.execute(httppost);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

Toast.makeText(this, br.readLine(), Toast.LENGTH_LONG).show();

服务器端检索和存储图像

String username = req.getParameter("username");
resp.getWriter().write(username);

try
{
byte[] image = req.getParameter("image").getBytes();

Blob profilepic = new Blob(image);

Profile profile = new Profile(username,profilepic);

pm = PMF.get().getPersistenceManager();

pm.currentTransaction().begin();

pm.makePersistent(profile);

pm.currentTransaction().commit();

resp.getWriter().write("Data stored successfully");
}

服务器端发送图片

String username = req.getParameter("username");

pm = PMF.get().getPersistenceManager();
pm.currentTransaction().begin();

javax.jdo.Query query = pm.newQuery(Profile.class);
Collection<Profile> result = (Collection<Profile>) query.execute();

for (Profile result1 : result) 
{
    if(result1.getUsername().equalsIgnoreCase(username))
    {
        resp.getWriter().write(result1.getImage().toString());
        pm.currentTransaction().commit();
        break;              
    }

}
4

0 回答 0