Android:通过用户选择的选项将文件上传到服务器

时间:2016-06-21 10:59:23

标签: java php android eclipse android-studio

我正在尝试使用此代码段将文件上传到服务器,并且无法创建目录。请帮忙

    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
                private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
                private static int SELECT_FILE = 3;

                private static final int MEDIA_TYPE_IMAGE = 1;
                private static final int MEDIA_TYPE_VIDEO = 2;

                private Uri fileUri; // file url to store image/video

                private void OperationType() {
                    final CharSequence[] items = { "Video", "Gallery","Photo",
                            "Cancel" };

                    AlertDialog.Builder builder = new AlertDialog.Builder(AdvertiserDashBoard.this);
                    builder.setItems(items, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            if (items[item].equals("Video")) {
                                if (!isDeviceSupportCamera()) {
                                    Toast.makeText(getApplicationContext(),
                                            "Sorry! Your device doesn't support video camera\nplease choose Ads from your gallery",
                                            Toast.LENGTH_LONG).show();
                                    // will close the app if the device does't have camera
                                }else{
                                    recordVideo();
                                }


                            } else if (items[item].equals("Gallery")) {
                                chooseImageFile();
                            }else if (items[item].equals("Photo")) {
                                if (!isDeviceSupportCamera()) {
                                    Toast.makeText(getApplicationContext(),
                                            "Sorry! Your device doesn't support camera\nplease choose Ads from your gallery",
                                            Toast.LENGTH_LONG).show();
                                    // will close the app if the device does't have camera
                                }else{
                                    captureImage();
                                }
                            }
                            else if (items[item].equals("Cancel")) {
                                dialog.dismiss();
                            }
                        }
                    });
                    builder.show();
                }



                private void captureImage() {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                    // start the image capture Intent
                    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
                }

                private void chooseImageFile() {

android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    Intent intent = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    fileUri = getOutputMediaFileUri(SELECT_FILE);
                    intent.setType("*/*");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                    // start the image capture Intent
                    startActivityForResult(Intent.createChooser(intent, "Select File"),
                            SELECT_FILE);



                }


                /**
                 * Launching camera app to record video
                 */
                private void recordVideo() {
                    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
                    // set video quality
                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
                    // name
                    // start the video capture Intent
                    startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
                }


                private Uri getOutputMediaFileUri(int type) {
                    return Uri.fromFile(getOutputMediaFile(type));
                }


                private static File getOutputMediaFile(int type) {

                    // External sdcard location
                    File mediaStorageDir = new File(
                            Environment
                                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                            "images");

                    // Create the storage directory if it does not exist
                    if (!mediaStorageDir.exists()) {
                        if (!mediaStorageDir.mkdirs()) {
                            Log.d("TAG", "Oops! Failed create "
                                    + "images" + " directory");
                            return null;
                        }
                    }

                    // Create a media file name
                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                            Locale.getDefault()).format(new Date());
                    File mediaFile;
                    if (type == MEDIA_TYPE_IMAGE) {
                        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                                + "IMG_" + timeStamp + ".jpg");
                    } else if (type == MEDIA_TYPE_VIDEO) {
                        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                                + "VID_" + timeStamp + ".mp4");
                    }

            //        else if (type == SELECT_FILE) {
            //            mediaFile = new File(mediaStorageDir.getPath() + File.separator
            //                    + "VID_" + timeStamp + ".mp4");
            //        }


                    else {
                        return null;
                    }

                    return mediaFile;
                }

                private boolean isDeviceSupportCamera() {
                    if (getApplicationContext().getPackageManager().hasSystemFeature(
                            PackageManager.FEATURE_CAMERA)) {
                        // this device has a camera
                        return true;
                    } else {
                        // no camera on this device
                        return false;
                    }
                }



                @Override
                protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                    // if the result is capturing Image
                    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
                        if (resultCode == RESULT_OK) {
                            // successfully captured the image
                            // launching upload activity
                            launchUploadActivity(true);

                        } else if (resultCode == RESULT_CANCELED) {
                            // user cancelled Image capture
                            Toast.makeText(getApplicationContext(),
                                    "User cancelled image capture", Toast.LENGTH_SHORT)
                                    .show();

                        } else {
                            // failed to capture image
                            Toast.makeText(getApplicationContext(),
                                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                                    .show();
                        }

                    } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
                        if (resultCode == RESULT_OK) {
                            // video successfully recorded
                            // launching upload activity
                            launchUploadActivity(false);

                        } else if (resultCode == RESULT_CANCELED) {

                            // user cancelled recording
                            Toast.makeText(getApplicationContext(),
                                    "User cancelled video recording", Toast.LENGTH_SHORT)
                                    .show();

                        } else {
                            // failed to record video
                            Toast.makeText(getApplicationContext(),
                                    "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }
                }



                private void launchUploadActivity(boolean isImage){
                    Intent i = new Intent(AdvertiserDashBoard.this, UploadLiveActivity.class);
                    i.putExtra("filePath", fileUri.getPath());
                    i.putExtra("isImage", isImage);
                    startActivity(i);
                }
         @Override
            public void onSaveInstanceState(Bundle outState) {
                super.onSaveInstanceState(outState);
                outState.putBundle("newBundy", newBundy);
                outState.putParcelable("file_uri", fileUri);
                // Save the state of the WebView
                m_webview.saveState(outState);

            }

            @Override
            protected void onRestoreInstanceState(Bundle savedInstanceState) {
                super.onRestoreInstanceState(savedInstanceState);
                savedInstanceState.getBundle("newBundy");
                fileUri = savedInstanceState.getParcelable("file_uri");
                // Restore the state of the WebView
                m_webview.restoreState(savedInstanceState);


            }

触发文件的按钮选择类型

案例R.id.btnAdsSubmit:

                OperationType();


                break;

            default:

请纠正我,我不知道代码片段出了什么问题

此代码段的目标是从主要活动中选择文件并将该网址发送到第二个活动进行预览。从第二个活动用户可以触发上传按钮将文件上传到服务器。

0 个答案:

没有答案