将所选图像转换为字节数组并转换为字符串

时间:2013-06-07 09:32:03

标签: android client-server bytearray

我正在进行客户端 - 服务器通信。 用户可以从图库中选择图像。 选定的图像将保存在本地DB和服务器DB两个位置。 如果用户保存它,所选图像将保存到本地数据库中,路径为(String) 并且还应该保存到Server数据库中。 问题是我不知道如何将图像字节数组编码为String以将图像传递给服务器端。

本地数据库:图像 - >路径(字符串)(已完成) 服务器DB:图像 - >字节 - >字符串 - >发送到服务器

这是代码..

    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        id=extras.getInt("id");
        inputname.setText(extras.getString("name"));
        inputnote.setText(extras.getString("note"));

        image = extras.getString("blob");


        //Convert image into string to save path in local DB
        BitmapFactory.Options op=new BitmapFactory.Options();
        op.inSampleSize=8;
        yourSelectedImage = BitmapFactory.decodeFile(image, op);
        inputphoto.setImageBitmap(yourSelectedImage);

    }

如何在saveItem方法中设置blob ..?

private void saveItem() {

    // Client-Server - Start //////////////////////////////////////
    String name = inputname.getText().toString();
    String description = inputnote.getText().toString();
    // Encode the image file to String !! by using Base64
    String encodedImage = Base64.encodeToString(blob, Base64.DEFAULT);

    // Building Parameters
    List<NameValuePair> params1 = new ArrayList<NameValuePair>();
    params1.add(new BasicNameValuePair("name", name));
    params1.add(new BasicNameValuePair("description", description));
    params1.add(new BasicNameValuePair("photo",encodedImage));

    Log.v("log_tag", System.currentTimeMillis()+".jpg");


    // getting JSON Object
    // Note that create product url accepts POST method
    JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params1);

    // check log cat fro response
    Log.d("Create Response", json.toString());

    // check for success tag
    try {
        int success = json.getInt(TAG_SUCCESS);


        Log.v("log_tag", "In the try Loop" );

        if (success == 1) {
            // closing this screen
            finish();
        } else {
            // failed to create product
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

提前谢谢你。

1 个答案:

答案 0 :(得分:2)

要将图片转换为字符串,请使用以下代码。

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
yourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//this will convert image to byte[] 
byte[] byteArrayImage = baos.toByteArray(); 
// this will convert byte[] to string
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

现在,您有 encodedImage 图片字符串。

您的“saveItem()”代码如下所示。

private void saveItem() {

    // Client-Server - Start //////////////////////////////////////
    String name = inputname.getText().toString();
    String description = inputnote.getText().toString();
    // Encode the image file to String !! by using Base64
    //String encodedImage = Base64.encodeToString(blob, Base64.DEFAULT);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    yourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    //this will convert image to byte[] 
    byte[] byteArrayImage = baos.toByteArray(); 
    // this will convert byte[] to string
    String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

    // Building Parameters
    List<NameValuePair> params1 = new ArrayList<NameValuePair>();
    params1.add(new BasicNameValuePair("name", name));
    params1.add(new BasicNameValuePair("description", description));
    params1.add(new BasicNameValuePair("photo",encodedImage));

    Log.v("log_tag", System.currentTimeMillis()+".jpg");


    // getting JSON Object
    // Note that create product url accepts POST method
    JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params1);

    // check log cat fro response
    Log.d("Create Response", json.toString());

    // check for success tag
    try {
        int success = json.getInt(TAG_SUCCESS);


        Log.v("log_tag", "In the try Loop" );

        if (success == 1) {
            // closing this screen
            finish();
        } else {
            // failed to create product
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }