當我從圖庫中取出圖像並想要插入服務器時,它會將圖像放在零字節大小的位置。在表中,我有兩列,分別是rollno
和image
。我從共享首選項中獲得rollno
值,但它不會將rollno
置於數據庫表中,rollno
列顯示爲空。無法將圖像上傳到服務器,它顯示零字節?
幫我解決這個問題。
這裏是我的代碼
public class ImageEditActivity extends AppCompatActivity implements View.OnClickListener {
ImageView image;
Button btn_cancel_edit,btn_done_edit;
Bitmap bitmap;
SharedPreferences sp;
String rollno;
String KEY_Rollno = "rollno";
String KEY_IMAGE = "image";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_edit);
getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
image=(ImageView)findViewById(R.id.image);
bitmap=ProfileActivity.bitmap;
image.setImageBitmap(bitmap);
btn_done_edit = (Button) findViewById(R.id.btn_done_edit);
btn_cancel_edit = (Button) findViewById(R.id.btn_cancel_edit);
btn_done_edit.setOnClickListener(this);
btn_cancel_edit.setOnClickListener(this);
sp=getSharedPreferences("rajput",MODE_PRIVATE);
rollno=sp.getString("rollno",null);
}
@Override
public void onClick(View v) {
if(v == btn_done_edit){
uploadImage();
}
if(v == btn_cancel_edit){
onBackPressed();
}
}
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
private void uploadImage() {
String UPLOAD_URL ="http://aptronnoida.com/applock/image_insert.php";
final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.GET, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(ImageEditActivity.this, s , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(ImageEditActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Creating parameters
Map<String,String> params = new Hashtable<String, String>();
//Adding parameters
params.put(KEY_Rollno,rollno);
params.put(KEY_IMAGE, image);
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
public void onBackPressed(){
super.finish();
}
}
看起來你'使用'GET'來上傳圖片,這可能是一個壞主意。根據您的PHP版本,您可能會將整個請求的字節數限制在512個字符以內,這就是bast64編碼時的3KB圖像。嘗試使用POST來代替。 – theFunkyEngineer