下面列出了android.util.Base64#NO_WRAP 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
* @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;
}
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('"');
}
}
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();
}
/**
* Just to test if using the ByteArrayInputStream isn't missing something
*/
@Test
public void itDoesNotAddLineBreaks_whenBase64InputStream() throws IOException {
byte[] exampleString = "dGVzdA==".getBytes();
Base64InputStream inputStream =
new Base64InputStream(new ByteArrayInputStream(exampleString), Base64.NO_WRAP);
assertEquals("dGVzdA==", invokeConversion(inputStream));
}
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();
}
}
}
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();
}
}
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('"');
}
}
@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('"');
}
}