无法使用Android将图像上传到服务器

时间:2017-10-07 23:00:54

标签: java php android

我试图将图像发送到服务器。当我选择一个图像manualy从例如Whatsapp或它的工作相机。但是,当我尝试使用应用程序制作照片时,它无法正常工作。它在数据库中插入数据但不将照片放入服务器。

这是我的代码

 private void showCamera(){
        Intent callCamera = new Intent();
        callCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

        File photoFile = null;
        try{
            photoFile = createImageFile();
        } catch (IOException e){
            e.printStackTrace();
        }


        String authorities = "de.boellingstudio.cc4imageupload.fileprovider";
        Uri imageUri = FileProvider.getUriForFile(this,authorities,photoFile);
        callCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
       // callCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

        startActivityForResult(callCamera,ACTIVITY_START_CAMERA_APP);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
            Log.d("Demo Pic", "Picture is saved");
            // Get the dimensions of the View

            Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
            ivpic.setImageBitmap(bitmap);

        }
    }



private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyHHss").format(new Date());
    String imageFileName = "PIC" + timeStamp + "_";
    Toast.makeText(UploadActivity.this,imageFileName,Toast.LENGTH_LONG).show();

    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    storageDir.mkdir();
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

这是用于上传图片我使用Android Upload Service

public void uploadImage(View v){
        String name = edname.getText().toString().trim();
    try {
        String uploadid = UUID.randomUUID().toString();
        new MultipartUploadRequest(this, uploadid, UPLOAD_URL)
        .addFileToUpload(mCurrentPhotoPath, "image")
        .addParameter("name",name)
        .setNotificationConfig(new UploadNotificationConfig())
        .setMaxRetries(2)
        .startUpload();
        Toast.makeText(UploadActivity.this,mCurrentPhotoPath,Toast.LENGTH_LONG).show();
        System.out.println(mCurrentPhotoPath);
    }catch (Exception e){
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

0 个答案:

没有答案