•  


Images getting corrupted on upload to bucket · Issue #5843 · firebase/firebase-android-sdk · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images getting corrupted on upload to bucket #5843

Open
pratham-PDots opened this issue Apr 8, 2024 · 3 comments
Open

Images getting corrupted on upload to bucket #5843

pratham-PDots opened this issue Apr 8, 2024 · 3 comments

Comments

@pratham-PDots
Copy link

[READ] Step 1: Are you in the right place?

Yes.

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: Android Studio Giraffe | 2022.3.1 Patch 2
  • Firebase Component: Storage
  • Component version: 20.2.1

[REQUIRED] Step 3: Describe the problem

This issue occurs randomly with some uploaded images i.e. it does not occur with every uploaded image so have not been able to reproduce but have faced it in production.

Have also noticed that the size of all such corrupted images have size as multiple of 256 KB (for example 512 KB, 1536 KB etc.)

Steps to reproduce:

General steps :

  1. Upload an image using the provided code.
  2. Check the uploaded image file in the Firebase Storage console or download it to a device.

Relevant Code:

package
 com.example.services


import
 android.app.Service

import
 android.content.Intent

import
 android.net.Uri

import
 android.os.IBinder

import
 android.util.Log

import
 com.google.firebase.storage.FirebaseStorage

import
 com.google.firebase.storage.StorageMetadata

import
 com.google.firebase.storage.StorageReference

import
 java.io.File


class
 MyService
 : 
Service
() {

    
private
 var
 storage
:
 FirebaseStorage
?
 =
 null

    private
 var
 storageReference
:
 StorageReference
?
 =
 null


    override
 fun
 onBind
(
intent
:
 Intent
?
): 
IBinder
?
 {
        
return
 null

    }

    
override
 fun
 onStartCommand
(
intent
:
 Intent
?
, 
flags
:
 Int
, 
startId
:
 Int
): 
Int
 {
        
//
 Initialize Firebase storage

        storage 
=
 FirebaseStorage
.getInstance()
        storageReference 
=
 storage
!!
.reference

        
//
 Dummy list of image URIs

        val
 imageList 
=
 listOf
(
            
"
uri1
"
,
            
"
uri2
"
,
            
"
uri3
"

        )

        
//
 Upload images to Firebase

        if
 (imageList.isNotEmpty()) {
            uploadImages(imageList)
        }

        
return
 START_STICKY

    }

    
private
 fun
 uploadImages
(
imageList
:
 List
<
String
>) {
        imageList.forEach { uri 
->

            val
 fileUri
:
 Uri
 =
 Uri
.parse(
"
file://
$uri
"
)

            
//
 Set metadata for the uploaded image

            val
 metadata 
=
 StorageMetadata
.
Builder
()
                .setContentType(
"
image/jpeg
"
)
                
//
 Add custom metadata as needed


            //
 Upload image to Firebase Storage

            val
 uploadTask 
=
 storageReference?.child(uri)?.putFile(fileUri, metadata.build())

            
//
 Handle upload success, failure, and progress

            uploadTask?.addOnSuccessListener { taskSnapshot 
->

                //
 Handle successful upload

                //
 You can get the download URL if needed:

                val
 downloadUrl 
=
 taskSnapshot.metadata?.reference?.downloadUrl
                
Log
.i(
"
MyService
"
, 
"
Image uploaded successfully
"
)

                
//
 Remove the uploaded image from local storage

                removeImageFromLocalStorage(uri)
            }?.addOnFailureListener { exception 
->

                //
 Handle failed upload

                Log
.e(
"
MyService
"
, 
"
Image upload failed: 
${exception.message}
"
)
            }?.addOnProgressListener { progress 
->

                //
 Handle upload progress

                val
 progressPercent 
=
 100.0
 *
 progress.bytesTransferred 
/
 progress.totalByteCount
                
Log
.i(
"
MyService
"
, 
"
Upload progress: 
$progressPercent
%
"
)
            }
        }
    }

    
private
 fun
 removeImageFromLocalStorage
(
uri
:
 String
) {
        
val
 file 
=
 File
(uri)
        
if
 (file.exists()) {
            
val
 isDeleted 
=
 file.delete()
            
Log
.d(
"
MyService
"
, 
"
Deleted image from local storage: 
$isDeleted
"
)
        }
    }

    
override
 fun
 onDestroy
() {
        
super
.onDestroy()
    }
}
@lehcar09
Copy link

Hi ,thank you for reaching out. I tried reproducing the issue but I was unable to encounter the behavior. I used the code you shared and uploaded images with the size of more than 1MB and all images were uploaded without an issue.

Are you constantly experiencing this error or does the issue occur on certain devices and/or android versions? I’m thinking if network or device resource allocation is a factor to the issue.

Could you try reproducing the issue using our quickstart app? Aside from that, have you tried using a work manager to manage the upload task?

@pratham-PDots
Copy link
Author

pratham-PDots commented Apr 12, 2024

Hi @lehcar09 , thankyou for looking into the issue.

These are received randomly, 1 out of say 100 images comes out as corrupted, as for devices, mostly have observed in Galaxy A53 5G (Android 14) but have gotten on some other devices as well.

  • Could you tell me why network or resource allocation would be a factor here? And how I could look into that?

  • Also the corrupted images always have a size i.e. a multiple of 256 KB, was thinking if that has something to do with firebase chunk size?

As for reproducing it, I have never been able to reproduce it either, only received corrupted images from users, will look into work manager though, would that help fix the issue you think?

Attaching a corrupted image for reference :
d1b4fb29-b028-418c-8384-7cf4d619fbe0_1

@pratham-PDots
Copy link
Author

Hi @lehcar09 , could you provide some info on the above questions? Would help debug the issue on my end, thanks!

Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본