2015-07-03 84 views
0

我正在模块中,用户可以将图像上传到服务器。为了实现这一点,我必须将选定的图像更改为Base64。转换后,我不得不使用JSON POST方法上载图片,但每次应用程序崩溃,我在这行得到错误..发布在Android上的PHP服务器上发布图像

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); 

回答

1

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

+0

谢谢你的帮助哥们,我已经试过你的推荐示例。它将位图转换为base64。但它显示Log.e(“LOOK”,imageEncoded)这么多行。 – Devraj

+0

您可以删除'Log.e(“LOOK”,imageEncoded)''行。它对功能没有任何影响。 – TmKVU

+0

是啊!!这是对的伙计。但我的问题是关于许多方面。我的意思是我只选择了一个图像,但它给了我很多base64代码行。 – Devraj