【问题标题】:Issue in Posting Image on php Server in android在 android 的 php 服务器上发布图像的问题
【发布时间】:2015-07-03 12:39:34
【问题描述】:

我正在开发一个模块,用户可以在该模块中将图像上传到服务器。为此,我必须将所选图像更改为 Base64。转换后,我必须使用 Json POST 方法来上传图像,但是每次应用程序崩溃并且我在这一行得到错误..

String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

这是我正在尝试的代码,请看一下,让我看看我在这里犯了什么错误。

buttonLoadPicture.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent i = new Intent(
                    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

}

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        ss = BitmapFactory.decodeFile(picturePath);
        Log.d("value", ss.toString());

  Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),ss);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

【问题讨论】:

    标签: android base64 android-image


    【解决方案1】:

    encodeToString 不是BitmapFactory 中的函数。您应该以不同的方式编码为 base64。我想建议this answer

    【讨论】:

    • 感谢您的帮助好友,我已经尝试了您建议的示例。它将位图转换为base64。但它显示了这么多行 Log.e("LOOK", imageEncoded);
    • 你可以删除Log.e("LOOK", imageEncoded);这一行。它不会对功能产生任何影响。
    • 是的!!.. 是的,伙计。但我的问题是关于多行的。我的意思是我只选择了一张图片,但它给了我这么多 base64 代码行。
    • @Devraj 当然会。图像通常由相对较多的字节组成。如果使用 Base64 对这些字节进行编码,则会收到许多字符。每个字节由 8 位组成,每个 Base64 字符由 7 位组成,因此是字符数。
    • 兄弟,现在我有另一个问题,当我试图在服务器上发布图像时,应用程序崩溃了。我已经更新了我的问题,请看一下。
    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多