如何通过Volley将大于2mb的图像文件存储到数据库中?

时间:2019-02-11 14:07:10

标签: java php android

我有一个代码,用于通过Volley将图像从移动设备上传到我的phpmyadmin数据库。如果格式为小“ .jpg”且尺寸不超过2mb,我可以成功上传图像。现在,当我选择的图片(尤其是从相机图片中获取的图片)更多时,它不会提示任何错误,也不会将图片添加到数据库中。我该如何解决以及如何存储?

这是我的settingsprofilefragment.java

public class SettingsFragment extends  Fragment{

private static final String TAG = SettingsFragment.class.getSimpleName();
private Button btnUploadVerificationPic;

private ImageView verificationPic;

private Bitmap bitmap;

TextView logout;


private static final String URL_UPLOADPIC = "http://isalonbyageeks.000webhostapp.com/uploadVerificationPhoto.php";

SessionManager sessionManager;
String getId = "";

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_settingsprofile, container, false);

    return view;


}

public void onViewCreated(View view, @Nullable Bundle savedInstanceState){

    sessionManager = new SessionManager(getActivity());
    sessionManager.checkLogin();

    logout = (TextView) getView().findViewById(R.id.txtLogout);
    btnUploadVerificationPic = (Button) getView().findViewById(R.id.uploadVeriPhoto);
    verificationPic = (ImageView) getView().findViewById(R.id.verificationPic);

    HashMap<String, String> user = sessionManager.getUserDetail();
    getId = user.get(sessionManager.ID);

    btnUploadVerificationPic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chooseFile();
        }
    });

    logout.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), LogoutEffect.class);
            sessionManager.logout();
            startActivity(intent);
        }
    });

}

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

public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1 && resultCode == RESULT_OK && data!= null && data.getData() != null){
        Uri filepath = data.getData();
        try {

            bitmap = (Bitmap) MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), filepath);
            verificationPic.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

        UploadPicture(getId, getStringImage(bitmap));

    }

}

private void UploadPicture(final String id, final String photo) {
    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Uploading...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_UPLOADPIC,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    Log.i(TAG, response);
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");

                        if(success.equals("1")){
                            Intent intent = new Intent(getActivity(), ProfileActivity.class);
                            startActivity(intent);
                            Toast.makeText(getActivity(),"Success!",Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        progressDialog.dismiss();
                        Toast.makeText(getActivity(),"Try Again! " + e.toString(),Toast.LENGTH_SHORT).show();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(getActivity(),"Try Again!" + error.toString(),Toast.LENGTH_SHORT).show();
                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("id",id);
            params.put("photo",photo);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(stringRequest);

}

public String getStringImage(Bitmap bitmap){
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,70 , byteArrayOutputStream);
    byte[] imageByteArray = byteArrayOutputStream.toByteArray();
    String encodedImage = Base64.encodeToString(imageByteArray, Base64.DEFAULT);
    return encodedImage;
}

  }

0 个答案:

没有答案