1

I'm working on an Android Code Labs challenge (the first one) , that requires you to take a screen shot of an activity and emailing it to someone.

There's only one activity and it displays "Hello World".

So I decided to use API class PixelCopy - specifically the method:

PixelCopy.request(Window source, Bitmap dest, PixelCopy.OnPixelCopyFinishedListener listener, Handler listenerThread)

I understand that PixelCopy.OnPixelCopyFinishedListener provides this callback method: onPixelCopyFinished(int copyResult) where int copyResult is PixelCopy.SUCCESS etc if the copy operation was successful.

I'm getting stuck because I don't understand how I should pass PixelCopy.OnPixelCopyFinishedListener into PixelCopy.request(). I mean what does the variable listener refer to ? what is its type ? what should listener be ?)

I know that there is an easier way and that it to use API class Screenshot, Screenshot.capture() but I am a glutton for punishment.

The code (as far as I've got with it) is below:

package com.experiments.azeem;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Layout;
import android.view.PixelCopy;
import android.view.View;
import android.view.Window;

public class MainActivity extends AppCompatActivity {
    Bitmap bitmap;
    int width;
    int height;
    Window win;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View view = (View) findViewById(R.id.activityScreen);
        width = view.getWidth();
        height = view.getHeight();
        win = this.getWindow();
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        PixelCopy.OnPixelCopyFinishedListener listener;

        PixelCopy.request (win, bitmap, listener.onPixelCopyFinished() , new Handler())
    }


}

0 Answers0