2016-08-23 10 views
1

enter image description hereAndroid: Wie kann ich ein Foto von ImageView hochladen?

Hallo Leute, wie kann ich das Bild auf meinem Dateimanager auf meinem Webhost laden, wenn ich rent auf die Schaltfläche klicken? Ab jetzt fange ich nicht mit irgendetwas an, weil ich versucht habe, es so hochzuladen, wie ich es aus der Galerie hochgeladen habe. Es hat nicht funktioniert. Kannst du mir helfen, Jungs? Ab sofort gereinigt ich meinen Code und das ist es

@Override 
public void onClick(View v) { 



    HashMap postData = new HashMap(); 

    postData.put("txtCarModel", tvCarModel.getText().toString()); 
    postData.put("txtCarType", tvCarType.getText().toString()); 
    postData.put("txtCapacity", tvCapacity.getText().toString()); 
    postData.put("txtPlateNumber", tvPlateNumber.getText().toString()); 
    postData.put("image", toString()); 
    postData.put("txtFuelType", tvFuelType.getText().toString()); 
    postData.put("txtOwner", tvPoster.getText().toString()); 


    if (TextUtils.isEmpty(etResDate.getText().toString())) { 
     Toast.makeText(this, "Insert reservation date.", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    postData.put("txtResDate", etResDate.getText().toString()); 

    if (TextUtils.isEmpty(etResTime.getText().toString())) { 
     Toast.makeText(this, "Insert reservation time.", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    postData.put("txtResTime", etResTime.getText().toString()); 

    if (TextUtils.isEmpty(etResLocation.getText().toString())) { 
     Toast.makeText(this, "Insert pickup location.", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    postData.put("txtResLocation", etResLocation.getText().toString()); 


    postData.put("txtRenter", pref.getString("username","").toString()); 


    PostResponseAsyncTask taskPost = new PostResponseAsyncTask(DetailActivity.this, postData, new AsyncResponse() { 
     @Override 
     public void processFinish(String s) { 
      if (s.contains("New records created successfully")) { 
       Log.d(TAG, s); 
       Toast.makeText(DetailActivity.this, "Wait for owners approval", Toast.LENGTH_SHORT).show(); 
       Intent in = new Intent(DetailActivity.this, RenterTabs.class); 
       startActivity(in); 
       finish(); 
      } else { 
       Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    }); 

    taskPost.execute("http://carkila.esy.es/rent.php"); 
} 

Antwort

0

private void selectImage() {

final CharSequence[] options = { "Take Photo", "Choose from Gallery", 
      "Cancel" }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(
      CustomerRegistration.this); 

    builder.setTitle("Add Photo!"); 

    builder.setItems(options, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int item) { 

      if (options[item].equals("Take Photo")) 

      { 

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

       File f = new File(android.os.Environment 
         .getExternalStorageDirectory(), "temp.jpg"); 

       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 

       startActivityForResult(intent, 1); 

      } 

      else if (options[item].equals("Choose from Gallery")) 

      { 

       Intent intent = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(intent, 2); 

      } 

      else if (options[item].equals("Cancel")) { 

       dialog.dismiss(); 

      } 

     } 

    }); 

    builder.show(); 

} 


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

    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK) { 

     if (requestCode == 1) { 

      File f = new File(Environment.getExternalStorageDirectory() 
        .toString()); 

      for (File temp : f.listFiles()) { 

       if (temp.getName().equals("temp.jpg")) { 

        f = temp; 

        break; 

       } 

      } 

      try { 

       Bitmap bitmap; 

       BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 
       bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 
         bitmapOptions); 

       // Rorate to portraite 
       Matrix matrix = new Matrix(); 
       matrix.postRotate(getImageOrientation(f.getAbsolutePath())); 
       bitmap = Bitmap 
         .createBitmap(bitmap,0, 0, bitmap.getWidth(), 
           bitmap.getHeight(), matrix, true); 
       // End rotrate to portait 
      //set Image view   
       img_user_photo.setImageBitmap(bitmap); 




       String path = android.os.Environment 

       .getExternalStorageDirectory() 

       + File.separator 

       + "Phoenix" + File.separator + "default"; 

       f.delete(); 

       OutputStream outFile = null; 

       File file = new File(path, String.valueOf(System 
         .currentTimeMillis()) + ".jpg"); 

       try { 

        outFile = new FileOutputStream(file); 

        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 

        outFile.flush(); 

        outFile.close(); 

       } catch (FileNotFoundException e) { 

        e.printStackTrace(); 

       } catch (IOException e) { 

        e.printStackTrace(); 

       } catch (Exception e) { 

        e.printStackTrace(); 

       } 

      } catch (Exception e) { 

       e.printStackTrace(); 

      } 

     } else if (requestCode == 2) { 

      Uri selectedImage = data.getData(); 

      String[] filePath = { MediaStore.Images.Media.DATA }; 

      Cursor c = getContentResolver().query(selectedImage, filePath, 
        null, null, null); 

      c.moveToFirst(); 

      int columnIndex = c.getColumnIndex(filePath[0]); 

      String picturePath = c.getString(columnIndex); 

      c.close(); 

      Bitmap bitmap = (BitmapFactory.decodeFile(picturePath)); 

      // Rorate to portraite 
      Matrix matrix = new Matrix(); 
      matrix.postRotate(getImageOrientation(picturePath)); 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), 
        bitmap.getHeight(), matrix, true); 


      //set Image view   
      img_user_photo.setImageBitmap(bitmap); 



     } 

    } 

} 

// ***** Rufen Sie einfach Methode selectImage() ****** img_user_photo // Bildansicht

+0

Hallo Herr, Entschuldigung für die späte Antwort Ich war die ganze Zeit in der Schule. Kann dieser Code zum Hochladen unter http://carkila.esy.es/uploadRent/id.jpg verwendet werden? Danke, mein Herr –