2017-08-28 55 views
0

當我從圖庫中取出圖像並想要插入服務器時,它會將圖像放在零字節大小的位置。在表中,我有兩列,分別是rollnoimage。我從共享首選項中獲得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(); 
    } 


} 
+0

看起來你'使用'GET'來上傳圖片,這可能是一個壞主意。根據您的PHP版本,您可能會將整個請求的字節數限制在512個字符以內,這就是bast64編碼時的3KB圖像。嘗試使用POST來代替。 – theFunkyEngineer

回答

0

請嘗試以下工作代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    private Button buttonChoose; 
    private Button buttonUpload; 

    private ImageView imageView; 

    private EditText editTextName; 

    private Bitmap bitmap; 

    private int PICK_IMAGE_REQUEST = 1; 

    private String UPLOAD_URL = "http://coderzheaven.com/sample_file_upload/upload_image.php"; 

    private String KEY_IMAGE = "image"; 
    private String KEY_NAME = "name"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.demo); 

     buttonChoose = (Button) findViewById(R.id.buttonChoose); 
     buttonUpload = (Button) findViewById(R.id.buttonUpload); 

     editTextName = (EditText) findViewById(R.id.editText); 

     imageView = (ImageView) findViewById(R.id.imageView); 

     buttonChoose.setOnClickListener(this); 
     buttonUpload.setOnClickListener(this); 
    } 

    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() { 
     //Showing the progress dialog 
     final ProgressDialog loading = ProgressDialog.show(this, "Uploading...", "Please wait...", false, false); 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, 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(MainActivity.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(MainActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show(); 
        } 
       }) { 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       //Converting Bitmap to String 
       String image = getStringImage(bitmap); 

       //Getting Image Name 
       String name = editTextName.getText().toString().trim(); 

       //Creating parameters 
       Map<String, String> params = new Hashtable<String, String>(); 

       //Adding parameters 
       params.put(KEY_IMAGE, image); 
       params.put("Roll_NO", "5"); 

       //returning parameters 
       return params; 
      } 
     }; 

     //Creating a Request Queue 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 

     //Adding request to the queue 
     requestQueue.add(stringRequest); 
    } 

    private void showFileChooser() { 
     Intent intent = new Intent(); 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { 
      Uri filePath = data.getData(); 
      try { 
       //Getting the Bitmap from Gallery 
       bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath); 
       //Setting the Bitmap to ImageView 
       imageView.setImageBitmap(bitmap); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    @Override 
    public void onClick(View v) { 

     if (v == buttonChoose) { 
      showFileChooser(); 
     } 

     if (v == buttonUpload) { 
      uploadImage(); 
     } 
    } 
} 

參考來源:http://www.coderzheaven.com/2011/04/25/android-upload-an-image-to-a-server/

相關問題