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