2016-08-23 77 views
1

enter image description hereAndroid:如何從ImageView上傳照片?

嗨,大家好,我點擊按鈕rent時,如何將圖片上傳到我的網絡主機上的文件管理器?截至目前,我不會從任何事情開始,因爲我試圖以我從圖庫上傳的方式上傳它,但它沒有奏效。你能幫助我嗎?截至目前我打掃我的代碼,這是它

@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"); 
} 

回答

0

私人無效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); 



     } 

    } 

} 

// *****只需調用方法selectImage()****** img_user_photo //圖片視圖

+0

嗨,先生,對不起,我遲到了,我一直在學校。此代碼是否可用於上傳'http:// carkila.esy.es/uploadRent/id.jpg'?謝謝先生 –