4

我想借助意图发送捕获的图像并将图像发送到远程服务器。我正在使用以下代码:

String image_str;
String URL =**************/image.php?;
ArrayList<NameValuePair> nameValuePairs;

imageview.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        TakePhoto();

    }
});

private void TakePhoto() {

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent, 0);

}

BitmapFactory.Options btmapOptions;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 0 && resultCode == RESULT_OK) {
        if (data != null) {

            /*  photo = (Bitmap) data.getExtras().get("data");

            imageview.setImageBitmap(photo); *//* this is image view where you want to set image*/

            Log.d("camera ---- > ", "" + data.getExtras().get("data"));

            Toast.makeText(getApplicationContext(), getLastImageId(), Toast.LENGTH_LONG).show();
            btmapOptions = new BitmapFactory.Options();

            photo = BitmapFactory.decodeFile( getLastImageId(), btmapOptions);
            imageview.setImageBitmap(photo);

        }
        // sendImg();
        dialog = ProgressDialog.show(surakhaActivity.this, "", "Uploading file...", true);
        new Thread(new Runnable() {
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {

                    }
                });                      
                sendImg();
                //  Toast.makeText(getBaseContext(), response, Toast.LENGTH_LONG).show();

            }
        }).start();    
    }
}

private String getLastImageId() {
    final String[] imageColumns = { MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
    Cursor imageCursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
                null, null, imageOrderBy);
    if (imageCursor.moveToFirst()) {
        int id = imageCursor.getInt(imageCursor
                    .getColumnIndex(MediaStore.Images.Media._ID));
        String fullPath = imageCursor.getString(imageCursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));

        imageCursor.close();
        return fullPath;
    } else {
        return "no path";
    }
}

InputStream inputStream;
File f;
public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException {

    String res = "";
    StringBuffer buffer = new StringBuffer();
    inputStream = response.getEntity().getContent();
    int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
    Toast.makeText(surakhaActivity.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
    if (contentLength < 0) {
    }
    else {
        byte[] data = new byte[512];
        int len = 0;
        try {
            while (-1 != (len = inputStream.read(data)) ) {
                buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        try {
            inputStream.close(); // closing the stream…..
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        res = buffer.toString();     // converting stringbuffer to string…..

        //  Toast.makeText(MainActivity.this, "Result : " + res, Toast.LENGTH_LONG).show();
    }
    return res;
}

public void sendImg() {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    photo.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.

    byte [] byte_arr =  stream.toByteArray();
        image_str = Base64.encodeBytes(byte_arr);
        nameValuePairs = new  ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("imgdata",image_str));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(URL);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            the_string_response = convertResponseToString(response);

            // editor.putString("imgRes", the_string_response);editor.commit();
            Toast.makeText(surakhaActivity.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
        } 
        catch(Exception e) {
            Toast.makeText(surakhaActivity.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();

        }


        dialog.dismiss();   
        this.finish();
    }
}

但是捕获图像和发送进度对话框会无限运行,并且不会发送图像。

4

1 回答 1

0

这看起来很腥...

dialog = ProgressDialog.show(surakhaActivity.this, "", "Uploading file...", true);
            new Thread(new Runnable() {
                   public void run() {
                        runOnUiThread(new Runnable() {
                               public void run() {

                               }
                           });                      
                        sendImg();
                  //  Toast.makeText(getBaseContext(), response, Toast.LENGTH_LONG).show();

                   }
                 }).start();

你能在 sendImg() 方法上放一个“Log”,看看它是否被调用过吗?

于 2013-07-06T04:50:11.783 回答