-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageUploadService.java
143 lines (126 loc) · 5.98 KB
/
ImageUploadService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import android.app.IntentService;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.vishwas.credit.application.Constants;
import com.vishwas.credit.application.utils.DateUtils;
import com.vishwas.credit.application.utils.Logger;
import com.vishwas.credit.application.utils.ValidationUtils;
import com.vishwas.credit.imageUpload.model.domain.ImageUploadObject;
import java.io.ByteArrayOutputStream;
/**
* Created by Ashish on 24/11/17.
*/
public class ImageUploadService extends IntentService {
private StorageReference storageReference;
private Bitmap bitmap;
public ImageUploadService() {
super("ImageUploadService");
FirebaseStorage.getInstance().setMaxUploadRetryTimeMillis(10000);
storageReference = FirebaseStorage.getInstance()
.getReference();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Logger.logError("starting imageupload");
ImageUploadObject imageUploadObject = intent.getParcelableExtra(Constants.BUNDLE_KEYS.IMAGE_UPLOAD_OBJECT);
initiateUpload(imageUploadObject);
}
@Override
public void onCreate() {
super.onCreate();
}
private void initiateUpload(ImageUploadObject imageUploadObject) {
uploadImageFileToFireBaseBackend(imageUploadObject);
}
private void uploadImageFileToFireBaseBackend(final ImageUploadObject imageUploadObject) {
Uri uri = Uri.parse(imageUploadObject.getImageUrl());
StorageReference ticketReference = storageReference.child(imageUploadObject.getFarmerId() + "");
final StorageReference fileReference =
ticketReference.child(DateUtils.epochToDateTime(imageUploadObject.getTimestamp()).replaceAll("/", ""));
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap = BitmapUtils.getDownsampledBitmap(this, uri, 512, 512);
bitmap.compress(Bitmap.CompressFormat.WEBP, 25, bytes);
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "CreditApp" + DateUtils.getCurrentMillis(), null);
UploadTask uploadTask;
if (!ValidationUtils.isEmpty(path)) {
uri = Uri.parse(path);
uploadTask = fileReference.putFile(uri);
} else {
uploadTask = fileReference.putBytes(bytes.toByteArray());
}
//Recycle bitmap..dont need this anymore..variables hold bytes or uri
bitmap.recycle();
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Logger.logError("Image Upload success");
imageUploadObject.setImageUrl(taskSnapshot.getDownloadUrl().toString());
imageUploadObject.setStatus(Constants.ImageUploadStates.IMAGE_UPLOAD_SUCCESS_STATUS);
sendBroadCastForImageUpload(imageUploadObject);
}
});
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(final UploadTask.TaskSnapshot taskSnapshot) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Logger.logError("Image Upload in progress");
long bytesTransferred = taskSnapshot.getBytesTransferred();
long totalByteCount = taskSnapshot.getTotalByteCount();
long l = (bytesTransferred / totalByteCount) * 100;
Logger.logError("Uploaded " + bytesTransferred / 1024 + " of " + totalByteCount);
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull final Exception exception) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
imageUploadObject.setStatus(-1);
Logger.logError("Image upload failed " + exception.getLocalizedMessage() + " Timestamp " + imageUploadObject.getTimestamp());
imageUploadObject.setStatus(Constants.ImageUploadStates.IMAGE_UPLOAD_FAILED_STATUS);
sendBroadCastForImageUpload(imageUploadObject);
}
});
}
});
}
private void sendBroadCastForImageUpload(ImageUploadObject imageUploadObject) {
Intent intent = new Intent();
intent.setAction(Constants.INTENT_ACTIONS.ACTION_IMAGE_UPLOAD_STATUS_UPDATE);
intent.putExtra(Constants.BUNDLE_KEYS.IMAGE_UPLOAD_OBJECT, imageUploadObject);
sendBroadcast(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}