Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8546961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:09:22+00:00 2026-06-11T13:09:22+00:00

I have some Java code that takes a screen shot and outputs a png,

  • 0

I have some Java code that takes a screen shot and outputs a png, that is meant to be saved to the phones ‘pictures’ gallery. It looks like this:

    package org.apache.cordova;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;



public class Screenshot extends Plugin {

    @Override

    public PluginResult execute(String action, JSONArray args, String callbackId) {
        // starting on ICS, some WebView methods
        // can only be called on UI threads
        final Plugin that = this;
        final String id = callbackId;
        super.cordova.getActivity().runOnUiThread(new Runnable() {
            //@Override
            public void run() {
                View view = webView.getRootView();

                view.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
                view.setDrawingCacheEnabled(false);

                try {
                    File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
                    if (!folder.exists()) {
                        folder.mkdirs();

                    }

                    File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");
                    System.out.println(folder);
                    System.out.println("screenshot_" + System.currentTimeMillis() + ".png");
                    FileOutputStream fos = new FileOutputStream(f);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    that.success(new PluginResult(PluginResult.Status.OK), id);
                    System.out.println("get here?");

                } catch (IOException e) {
                    that.success(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage()), id);
                    System.out.println("and here?");
                }
            }
        });

        PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
        result.setKeepCallback(true);
        return result;
    }

}

Now for the purposes of the project I am working on, I would like to not save t to the gallery, but instead output a base64 string that can be manipulated. I am new to Java but after looking through some stuff came across a base64 Java encoder. But there seems to be an android util method called Base64OutputStream.

The code I think I need to replace is:

FileOutputStream fos = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

At first I tried:

Base64OutputStream os = new Base64OutputStream();

But it threw errors, then I have been playing about with other bits of code I found. Such as:

ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStream b64 = new Base64.OutputStream(os);
ImageIO.write(bi, "png", b64);
String result = os.toString("UTF-8");

It again didn’t work, instead of implementing a base64 encoding library for a small section of my project, I was wondering if anyone could point me in the right direction, or tell me if im on the right path. I am new to Java, but feel that the android provided Base64OutputStream must be the key. Anyone have any pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:09:23+00:00Added an answer on June 11, 2026 at 1:09 pm

    Try this code this should work for you…

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();
            base64String = Base64.encodeToString(b, Base64.DEFAULT);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Java code that takes an XML (SOAP) message and returns the
I have some Java code that is throwing out of memory exceptions after running
I have some Java code that connects to an Oracle database using DriverManager.getConnection(). It
I have a java code that looks like this: //UI thread //Some code Job
We have a system that has some Bash scripts running besides Java code. Since
I am attempting to write some Java code that takes an image from a
I have some image processing Java code in Android that acts upon two large
I have a some java code that simulates bank transfers. The account class simply
I'd like to write some (java) code that takes a PDF document, and creates
Presently I have a code that has a java main method and which takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.