当我尝试在android studio中创建一个新项目时遇到问题。我已经连接到MySQL数据库了。在数据库中有一个用户表,其中包含一个字段:用户名,密码和图像(类型数据是blob)。当我从MySQL手动添加(插入)数据到用户表,尤其是图像字段时,我在Android Studio上执行GetString,然后转换为位图。然后我设法显示图像。但我的问题是当我尝试从库中创建一个按钮选择然后我将位图插入到图片字段中的用户表然后我再次对我刚刚插入的图片进行GetString,但结果是图片没有出现,请解决我遇到的问题,谢谢。 这是我的PHP代码
<?php
$nm=$_POST['nama'];
$em=$_POST['email'];
$hm=$_POST['home'];
$ph=$_POST['phone'];
$gbr=$_POST['gambar'];
$pw=$_POST['pass'];
$conn = mysqli_connect("localhost","root","", "needme");
$query = "INSERT INTO user(email,name,Password,AddressHome,Phone,gambar)
values ('$em','$nm','$pw','$hm','$ph','$gbr')";
$result = mysqli_query($conn, $query) or die("REPORT failed DATA.");
if ($result){
echo '1';
}
else{
echo '0';
}
?>
这是我的java代码,btnReg是注册按钮
btnReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest strRequest = new StringRequest(com.android.volley.Request.Method.POST, URL_INSERT,
new Response.Listener<String>() {
@Override
public void onResponse(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(Registration2.this, "Register Success", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Registration2.this, Login.class);
startActivity(intent);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Registration2.this, "Register Failed", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams()
throws AuthFailureError {
// TODO Auto-generated method stub
bm = ((BitmapDrawable)iv.getDrawable()).getBitmap();
//byte[] bmm = i.getByteArrayExtra("gambar");
//Bitmap bitmap= BitmapFactory.decodeByteArray(image, 0, image.length);
ByteArrayOutputStream bit = new ByteArrayOutputStream();
boolean compress = bm.compress(Bitmap.CompressFormat.PNG, 100, bit);
byte[] byteImg = bit.toByteArray();
strImage = Base64.encodeToString(bit.toByteArray(), Base64.DEFAULT);
Map<String, String> params = new HashMap<String, String>();
params.put("nama", nama.getText().toString());
params.put("email", email.getText().toString());
params.put("home", home.getText().toString());
params.put("phone", phone.getText().toString());
params.put("gambar",strImage);
params.put("pass",pw.getText().toString());
return params;
}
};
RequestQueue reqQueue = Volley.newRequestQueue(Registration2.this);
reqQueue.add(strRequest);
}
});
这是我从画廊或相机中挑选后的代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
iv.setImageBitmap(thumbnail);
} else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(this, selectedImageUri, projection, null, null,
null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm2 = BitmapFactory.decodeFile(selectedImagePath, options);
bm1 = getResizedBitmap(bm2, 150, 150);
bm = ImageConverter.getRoundedCornerBitmap(bm1, 100);
//bm = GetBitmapClippedCircle(selectedImagePath);
iv.setImageBitmap(bm);
cropCenter(bm);
//int croppedBitmap;
}
//iv.setImageURI(selectedImageUri);
}
}