【问题标题】:Android: How to upload photo from ImageView?Android:如何从 ImageView 上传照片?
【发布时间】:2016-08-23 12:47:39
【问题描述】:

大家好,当我单击按钮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");
}

【问题讨论】:

    标签: android image upload


    【解决方案1】:

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

    // *****只需调用方法 selectImage() ****** img_user_photo //图片查看

    【讨论】:

    • 您好,先生,很抱歉回复晚了,我一直在学校。此代码可用于在http://carkila.esy.es/uploadRent/id.jpg 上传吗?谢谢先生
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多