在 android 的 php 服务器上发布图片时出现问题
Issue in Posting Image on php Server in android
我正在开发一个用户可以将图像上传到服务器的模块。为此,我必须将所选图像更改为 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);
encodeToString
不是 BitmapFactory. You should encode to base64 in a different way. I would like to suggest this answer
中的函数
我正在开发一个用户可以将图像上传到服务器的模块。为此,我必须将所选图像更改为 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);
encodeToString
不是 BitmapFactory. You should encode to base64 in a different way. I would like to suggest this answer