0

I am trying to capture an image via camera and save it to Firebase Storage (along with some text data). The text data is stored successfully in the Firebase Database. However upon saving the image in Storage I am getting the following exception

com.google.firebase.storage.StorageException: An unknown error occurred, please check the HTTP result code and inner exception for server response.

Here are my Gradle dependencies:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.websol.traceworker"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.annotation:annotation:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'com.google.firebase:firebase-storage:17.0.0'
    implementation 'com.google.firebase:firebase-database:16.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    testImplementation 'junit:junit:4.12'
    implementation platform('com.google.firebase:firebase-bom:28.4.1')
    implementation 'com.google.firebase:firebase-storage'
    implementation platform('com.google.firebase:firebase-bom:28.4.1')
    implementation 'com.google.firebase:firebase-analytics'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'

In the manifest file:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera" android:required="true" 

I have also written code for runtime permissions.

In onButtonClickListner() method: { if(permissionsGranted) dispatchTakePictureIntent(); }

The code for capturing picture is here:

       private void dispatchTakePictureIntent() {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            try {
                File dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File imgFile=new File(dir,GetPictureName());
                image_uri=Uri.fromFile(imgFile);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    
            } catch (ActivityNotFoundException e) {
              
            }
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
    
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                data.putExtra(MediaStore.EXTRA_OUTPUT,image_uri);
    
                imgUpload.setImageBitmap(imageBitmap); // imgUpload is an ImageView
saveToFirebase();
    
            }
        }

    private void saveToFirebase() {
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference storageRef = storage.getReferenceFromUrl("gs://smart-worker-c73c4.appspot.com/gs:");

// Create a reference to "file"
      DatabaseReference myRef = database.getReference("posts");
        String id=myRef.push().getKey();


          storageRef.child("images").putFile(image_uri)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            // Get a URL to the uploaded content
                            Uri uri = taskSnapshot.getUploadSessionUri();

                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {

                        }
                    });

        }

And here is a screenshot of my Storage Bucket: enter image description here

A lot of thanks in advance for your time and consideration.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The error message says "please check [...] inner exception for server response". Did you do that? What did the inner exception say? – Frank van Puffelen Sep 25 '21 at 16:22
  • It says: An unknown error occurred, please check the HTTP result code and inner exception for server response. Code: -13000 HttpResult: 0 2021-09-25 22:53:16.542 9688-9688/com.websol.traceworker E/StorageException: /storage/emulated/0/Pictures/img20210925_225300.jpg: open failed: ENOENT (No such file or directory) java.io.FileNotFoundException: /storage/emulated/0/Pictures/img20210925_225300.jpg: open failed: ENOENT (No such file or directory) @FrankvanPuffelen – Muzzammil Hussain Sep 25 '21 at 17:59
  • Cool. That means that no file exists at `/storage/emulated/0/Pictures/img20210925_225300.jpg` when you're trying to access it. What exact line does that error come from? – Frank van Puffelen Sep 25 '21 at 19:08
  • Also note that this line won't do what you seem to expect of it: `Uri uri = taskSnapshot.getUploadSessionUri();` See my answer here for an explantion why: https://stackoverflow.com/a/53653522/209103 – Frank van Puffelen Sep 25 '21 at 19:09
  • @FrankvanPuffelen got the reason. The file is not "physically" saved on the external storage. However I am getting the file created along with the Uri. Can you guide why this happens? – Muzzammil Hussain Sep 26 '21 at 17:49

0 Answers0