0

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?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • Add `return null;` in that catch block and let the caller check for null before trying to upload something that does not exist. – blackapps Mar 14 '21 at 19:45
  • 2
    `However, the photouploaded is completely black.` You should have a look on your device to display the file with a normal file manager. Then you know if the file is ok .You are talking about uploading. But the check if you can take a good screenshot comes first. You can also displayu the bitmap in an ImageView to check what you have. – blackapps Mar 14 '21 at 19:50
  • Seems like it's an issue with the bitmap. It's not returning a null bitmap, but it's not loading anything into my imageView. Right now for the ```screenShot(View view)``` method I'm using the root view layout (a constraint layout). – DIRTY DAVE Mar 14 '21 at 20:20
  • @blackapps Thanks for the help, it turned out to be an issue from OpenCV not being able to take a picture of the ```JavaCameraView``` but it works fine with other views. – DIRTY DAVE Mar 16 '21 at 16:44

0 Answers0