2

I want to take a picture from my android app, but i'm getting a black image because of my videoView in current Activity. How can i take it without root access?

bellow is the code to get my current View:

In my main Activity:

saveBitmap(tackeScreenshot());

And the methods used:

public Bitmap takeScreenshot() {
        View rootView = findViewById(android.R.id.content).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
    }


public void saveBitmap(Bitmap bitmap) {

        File imagePath = new File("/sdcard/image.png");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Functions.Log("saveBitmap", e.getMessage());
        } catch (IOException e) {
            Functions.Log("saveBitmap", e.getMessage());
        }
    }

1 Answers1

0

You should do something like this

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   


// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Then, when you need to access use something like this:

Uri uri = Uri.fromFile(new File(mPath));
Nauman Afzaal
  • 1,046
  • 12
  • 20