0

im trying to send an Image to an server. When i choose an image manualy fromfor example Whatsapp or the camera it works. But when i try to make a Photo with th App it wont work. It inserts the Data in the Database but dont put a Photo to the server.

This is my Code

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

This is for Uploading the image I use the 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();
    }
}
  • If you use `uploadImage` for any other image to which you have the path to, then the image is uploaded properly? But, when you take a picture with the camera and save the path to that image in the variable `mCurrentPhotoPath` then it doesn't work? Have you validated that 'mCurrentPhotoPath`is indeed the correct path to the image file and that the image file is a valid image? – Barns Oct 07 '17 at 23:16
  • In the `onActivityResult` you don't do anything other then set the image to an `ImageView`. You never send it to the server. At least, that is not evident from the code that you've posted. – Titus Oct 07 '17 at 23:19
  • @Barns52 If i put a Path from for example ".../DCIM/Camera/...jpg" it works. But"/Android/data/de.boellingstudio/cc....." wont work. Yes I checked tve mCurrentPhotoPath it is the right path and there is an Image. I uplaoad the Image with a Button. uploadImage is an OnClick – Dennis Bölling Oct 07 '17 at 23:40
  • I'm seeing bits and pieces of your code popping up in a lot of places and it seems like a lot of people are trying to get it to work somehow. https://stackoverflow.com/questions/37953476/android-how-to-get-a-content-uri-for-a-file-in-the-external-storage-public-d ::: https://stackoverflow.com/questions/39321746/fileprovider-doesnt-work-for-image-capture ::: Is there any reason why you what to save the image prior to returning from the camera activity? – Barns Oct 08 '17 at 01:03

0 Answers0