2015-07-03 86 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