Java Code Examples for android.util.Base64OutputStream

The following examples show how to use android.util.Base64OutputStream. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: weex   Source File: ScreencastDispatcher.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  if (!mIsRunning || mBitmap == null) {
    return;
  }
  int width = mBitmap.getWidth();
  int height = mBitmap.getHeight();
  mStream.reset();
  Base64OutputStream base64Stream = new Base64OutputStream(mStream, Base64.DEFAULT);
  // request format is either "jpeg" or "png"
  Bitmap.CompressFormat format = Bitmap.CompressFormat.valueOf(mRequest.format.toUpperCase());
  mBitmap.compress(format, mRequest.quality, base64Stream);
  mEvent.data = mStream.toString();
  mMetadata.pageScaleFactor = 1;
  mMetadata.deviceWidth = width;
  mMetadata.deviceHeight = height;
  mEvent.metadata = mMetadata;
  mPeer.invokeMethod("Page.screencastFrame", mEvent, null);
  mMainHandler.postDelayed(mEndAction, FRAME_DELAY);
}
 
Example 2
Source Project: stetho   Source File: ScreencastDispatcher.java    License: MIT License 6 votes vote down vote up
@Override
public void run() {
  if (!mIsRunning || mBitmap == null) {
    return;
  }
  int width = mBitmap.getWidth();
  int height = mBitmap.getHeight();
  mStream.reset();
  Base64OutputStream base64Stream = new Base64OutputStream(mStream, Base64.DEFAULT);
  // request format is either "jpeg" or "png"
  Bitmap.CompressFormat format = Bitmap.CompressFormat.valueOf(mRequest.format.toUpperCase());
  mBitmap.compress(format, mRequest.quality, base64Stream);
  mEvent.data = mStream.toString();
  mMetadata.pageScaleFactor = 1;
  mMetadata.deviceWidth = width;
  mMetadata.deviceHeight = height;
  mEvent.metadata = mMetadata;
  mPeer.invokeMethod("Page.screencastFrame", mEvent, null);
  mMainHandler.postDelayed(mEndAction, FRAME_DELAY);
}
 
Example 3
Source Project: Roid-Library   Source File: RLFileUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param file
 * @return
 */
public static String fileToBase64(String file) {
    String result= null;
    try {
        FileInputStream fis = new FileInputStream(new File(file));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Base64OutputStream base64out = new Base64OutputStream(baos, Base64.NO_WRAP);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) >= 0) {
            base64out.write(buffer, 0, len);
        }
        base64out.flush();
        base64out.close();
        /*
         * Why should we close Base64OutputStream before processing the data:
         * http://stackoverflow.com/questions/24798745/android-file-to-base64-using-streaming-sometimes-missed-2-bytes
         */
        result = new String(baos.toByteArray(), "UTF-8");
        baos.close();
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 4
Source Project: ans-android-sdk   Source File: ViewSnapshot.java    License: GNU General Public License v3.0 5 votes vote down vote up
public synchronized void writeBitmapJSON(Bitmap.CompressFormat format, int quality,
                                         OutputStream out)
        throws IOException {
    if (null == mCached || mCached.getWidth() == 0 || mCached.getHeight() == 0) {
        out.write("null".getBytes());
    } else {
        out.write('"');
        final Base64OutputStream imageOut = new Base64OutputStream(out, Base64.NO_WRAP);
        mCached.compress(Bitmap.CompressFormat.PNG, 100, imageOut);
        imageOut.flush();
        out.write('"');
    }
}
 
Example 5
Source Project: react-native-GPay   Source File: ImageStoreManager.java    License: MIT License 5 votes vote down vote up
String convertInputStreamToBase64OutputStream(InputStream is) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  Base64OutputStream b64os = new Base64OutputStream(baos, Base64.NO_WRAP);
  byte[] buffer = new byte[BUFFER_SIZE];
  int bytesRead;
  try {
    while ((bytesRead = is.read(buffer)) > -1) {
      b64os.write(buffer, 0, bytesRead);
    }
  } finally {
    closeQuietly(b64os); // this also closes baos and flushes the final content to it
  }
  return baos.toString();
}
 
Example 6
Source Project: RestVolley   Source File: JsonStreamerEntity.java    License: Apache License 2.0 5 votes vote down vote up
private void writeToFromStream(OutputStream os, StreamWrapper entry)
        throws IOException {

    // Send the meta data.
    writeMetaData(os, entry.name, entry.contentType);

    int bytesRead;

    // Upload the file's contents in Base64.
    Base64OutputStream bos = new Base64OutputStream(os, Base64.NO_CLOSE | Base64.NO_WRAP);

    // Read from input stream until no more data's left to read.
    while ((bytesRead = entry.inputStream.read(buffer)) != -1) {
        bos.write(buffer, 0, bytesRead);
    }

    // Close the Base64 output stream.
    if (bos != null) {
        bos.close();
    }

    // End the meta data.
    endMetaData(os);

    // Close input stream.
    if (entry.autoClose) {
        // Safely close the input stream.
        if (entry.inputStream != null) {
            entry.inputStream.close();
        }
    }
}
 
Example 7
Source Project: RestVolley   Source File: JsonStreamerEntity.java    License: Apache License 2.0 5 votes vote down vote up
private void writeToFromFile(OutputStream os, FileWrapper wrapper)
        throws IOException {

    // Send the meta data.
    writeMetaData(os, wrapper.file.getName(), wrapper.contentType);

    int bytesRead;

    // Open the file for reading.
    FileInputStream in = new FileInputStream(wrapper.file);

    // Upload the file's contents in Base64.
    Base64OutputStream bos = new Base64OutputStream(os, Base64.NO_CLOSE | Base64.NO_WRAP);

    // Read from file until no more data's left to read.
    while ((bytesRead = in.read(buffer)) != -1) {
        bos.write(buffer, 0, bytesRead);
    }

    // Close the Base64 output stream.
    if (bos != null) {
        bos.close();
    }

    // End the meta data.
    endMetaData(os);

    // Safely close the input stream.
    if (in != null) {
        in.close();
    }
}
 
Example 8
Source Project: Dream-Catcher   Source File: ResponseBodyFileManager.java    License: MIT License 5 votes vote down vote up
public OutputStream openResponseBodyFile(String requestId, boolean base64Encode)
    throws IOException {
  OutputStream out = mContext.openFileOutput(getFilename(requestId), Context.MODE_PRIVATE);
  out.write(base64Encode ? 1 : 0);
  if (base64Encode) {
    return new Base64OutputStream(out, Base64.DEFAULT);
  } else {
    return out;
  }
}
 
Example 9
Source Project: weex   Source File: ResponseBodyFileManager.java    License: Apache License 2.0 5 votes vote down vote up
public OutputStream openResponseBodyFile(String requestId, boolean base64Encode)
    throws IOException {
  OutputStream out = mContext.openFileOutput(getFilename(requestId), Context.MODE_PRIVATE);
  out.write(base64Encode ? 1 : 0);
  if (base64Encode) {
    return new Base64OutputStream(out, Base64.DEFAULT);
  } else {
    return out;
  }
}
 
Example 10
Source Project: stetho   Source File: ResponseBodyFileManager.java    License: MIT License 5 votes vote down vote up
public OutputStream openResponseBodyFile(String requestId, boolean base64Encode)
    throws IOException {
  OutputStream out = mContext.openFileOutput(getFilename(requestId), Context.MODE_PRIVATE);
  out.write(base64Encode ? 1 : 0);
  if (base64Encode) {
    return new Base64OutputStream(out, Base64.DEFAULT);
  } else {
    return out;
  }
}
 
Example 11
Source Project: sa-sdk-android   Source File: ViewSnapshot.java    License: Apache License 2.0 5 votes vote down vote up
public synchronized void writeBitmapJSON(Bitmap.CompressFormat format, int quality,
                                         OutputStream out)
        throws IOException {
    if (null == mCached || mCached.getWidth() == 0 || mCached.getHeight() == 0) {
        out.write("null".getBytes());
    } else {
        out.write('"');
        final Base64OutputStream imageOut = new Base64OutputStream(out, Base64.NO_WRAP);
        mCached.compress(Bitmap.CompressFormat.PNG, 100, imageOut);
        imageOut.flush();
        out.write('"');
    }
}
 
Example 12
Source Project: clevertap-android-sdk   Source File: SnapshotBuilder.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings({"SameParameterValue", "unused"})
synchronized void writeJSON(Bitmap.CompressFormat format, int quality, OutputStream out) throws IOException {
    if (cachedScreenshot == null || cachedScreenshot.getWidth() == 0 || cachedScreenshot.getHeight() == 0) {
        out.write("null".getBytes());
    } else {
        out.write('"');
        final Base64OutputStream imageOut = new Base64OutputStream(out, Base64.NO_WRAP);
        cachedScreenshot.compress(Bitmap.CompressFormat.PNG, 100, imageOut);
        imageOut.flush();
        out.write('"');
    }
}
 
Example 13
@Override
protected void setUp() throws Exception {
    super.setUp();

    Context context = this.getContext();
    PixateFreestyle.init(context.getApplicationContext());

    // Grab the bitmap placed in the assets. We can use it to compare
    // results later.
    InputStream is = context.getAssets().open(IMAGE_ASSET);
    assetBitmap = BitmapFactory.decodeStream(is);
    is.close();

    Resources resources = context.getResources();

    int rawFileId = resources.getIdentifier(RAW_TEST_FILE, "raw", this.getContext().getPackageName());

    testFileContents = readStream(resources.openRawResource(rawFileId));

    // Create a document file.
    OutputStreamWriter writer =
            new OutputStreamWriter(getContext().openFileOutput(DOCUMENT_FILE, Context.MODE_PRIVATE));
    try {
        writer.write(testFileContents);
    } finally {
        writer.close();
    }

    // Learn the document file's file:// uri so we can test that scheme.
    documentFileUri = new File(context.getFilesDir(), DOCUMENT_FILE).toURI().toString();

    // Clean it up to make it look like someone would type it in css
    // (file:// instead of just file:/)
    if (documentFileUri.startsWith("file:/") && !documentFileUri.startsWith("file://")) {
        documentFileUri = documentFileUri.replace("file:", "file://");
    }

    // Create a temp file.
    tempFile = new File(context.getCacheDir(), TMP_FILE);
    writer = new OutputStreamWriter(new FileOutputStream(tempFile));
    try {
        writer.write(testFileContents);
    } finally {
        writer.close();
    }

    // Get a base64 of the test asset image bytes so we can do a data: call
    // and compare results.
    is = context.getAssets().open(IMAGE_ASSET);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Base64OutputStream bos = new Base64OutputStream(output, Base64.DEFAULT);

    try {
        byte[] buffer = new byte[2048];
        int count = is.read(buffer);

        while (count > 0) {
            bos.write(buffer, 0, count);
            count = is.read(buffer);
        }

        assetBitmapBase64 = output.toString();

    } finally {
        is.close();
        bos.close();
    }

}