Android:如何从ImageView上传照片?

时间:2016-08-23 12:47:40

标签: android image upload

enter image description here

大家好,当我点击按钮InputFilter filter = new InputFilter() { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { if (Character.isWhitespace(source.charAt(i))) { return ""; } } return null; } }; editText.setFilters(new InputFilter[]{filter}); 时,如何将图像上传到我的webhost上的文件管理器?截至目前,我没有开始任何事情,因为我试图按照我从图库上传的方式上传它,它没有工作。你能帮帮我们吗?截至目前,我清理了我的代码,这就是它

rent

1 个答案:

答案 0 :(得分: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);



        }

    }

}

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