Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2017
    Messaggi
    2

    Upload su db da Android

    Salve a tutti, dovrei caricare un'immagine da android passando per php su un db mysql, mi servirebbe il codice lato server per prendere l'immagine e caricarla. Grazie mille.

    =============================================
    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://192.168.1.2/upload.php";


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


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


    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,"Caricamento immagine...","Per favore attendi...",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(KEY_NAME, name);


    //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 , "Seleziona il codice QR"), 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(getContentResolv er(), 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();
    }
    }
    }
    ===================================

  2. #2
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,445
    Benvenuto.
    Intanto il codice dell'app Android non interessa a PHP, basta che gli mandi dati in POST, il codice che c'è dietro non gli interessa.

    Per quanto riguarda la richiesta, su questo forum non funziona così, non si chiede codice bello e pronto per fare poi copia/incolla.
    Prima fai qualcosa, poi chiedi nello specifico cosa ti da problemi, mostrando il codice che hai scritto.

    Leggi il regolamento.

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.