代码之家  ›  专栏  ›  技术社区  ›  Peter

android下如何接收摄像头数据上传到php服务器

  •  1
  • Peter  · 技术社区  · 8 年前

    我试图创建android文件上传到php服务器,一切都在工作,除了当我选择相机拍摄上传照片,它没有收到的数据,我已经尝试了一切,我可以,但无法得到它写。我用的是安卓 gotev https://github.com/gotev/android-upload-service 我的文件上传。

    我想我的问题是 onActivityResult 接收cam数据,但我不知道怎么做。

    private Bitmap bitmap;
    private Uri filePath;
    private String CAM_MESSAGE;
    private ValueCallback<Uri> FILE_MESSAGE;
    
      @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           if (requestCode == IMAGE_REQUEST_CODE){
               if (resultCode == FileUploadActivity.RESULT_OK && data != null && data.getData() != null) {
                   filePath = data.getData();
                   try {
                       bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                       tvPath.setText("Path: ".concat(getPath(filePath)));
                       imageView.setImageBitmap(bitmap);
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
               } else {
                   Uri[] camfilePath = null;
                   if (CAM_MESSAGE != null) {
                       try {
                           camfilePath = new Uri[]{Uri.parse(CAM_MESSAGE)};
                           bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), camfilePath);
                           tvPath.setText("Path: ".concat(getPath(camfilePath)));
                           imageView.setImageBitmap(bitmap);
                           //Toast.makeText(getApplicationContext(), "CAM MESSAGE:"+CAM_MESSAGE, Toast.LENGTH_SHORT).show();
                       } catch (IOException e) {
                           e.printStackTrace();
                       }
                   }else{
                       Toast.makeText(getApplicationContext(), "Empty data", Toast.LENGTH_SHORT).show();
                   }
               }
           }else{
               Toast.makeText(getApplicationContext(), "Not Request Code", Toast.LENGTH_SHORT).show();
           }
        }
    

    private void showFileChooser(){
        File  photoFile = null;
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType(APP_FTYPE);
        Intent[] intentArray;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
        if (takePictureIntent.resolveActivity(FileUploadActivity.this.getPackageManager()) != null) {
            try {
                photoFile = createImage();
                takePictureIntent.putExtra("PhotoPath", CAM_MESSAGE);
            } catch (IOException ex) {
                Log.e(TAG, "Image file creation failed", ex);
            }
            if (photoFile != null) {
                CAM_MESSAGE = "file:" + photoFile.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            } else {
                takePictureIntent = null;
            }
        }
        if (takePictureIntent != null) {
            intentArray = new Intent[]{takePictureIntent};
        } else {
            intentArray = new Intent[0];
        }
    
        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
        startActivityForResult(chooserIntent, IMAGE_REQUEST_CODE);
    }
    
    0 回复  |  直到 8 年前
    推荐文章