I'm trying to take a screenshot of my app programmatically and convert it to a file for a multipart upload:
Taking screen shot:
https://stackoverflow.com/a/16109978/11110509
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Converting to a file for a multipart upload:
https://stackoverflow.com/a/41799811/11110509
private File createFile(Bitmap bitmap, String name) {
File filesDir = MainActivity.this.getFilesDir();
File imageFile = new File(filesDir, name + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
return imageFile;
}
My upload works perfectly fine to the server:
private void uploadImage() {
//mParentLayout is the rootview of the layout
File file = createFile(screenShot(mParentLayout), "testupload");
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body =
MultipartBody.Part.createFormData("avatar", file.getName(), requestFile);
Call<ResponseBody> call = service.uploadImage(Constants.USER_ID, body);
call.enqueue(new Callback<ResponseBody>() {
However, the photouploaded is completely black. I have tried it with selecting a photo, from my phone gallery and the upload is perfectly fine. So I'm assuming the issue is with taking a screenshot programatically, or the conversion of the bitmap to a file. Anybody know what I am doing it incorrectly?