Skip to content

Instantly share code, notes, and snippets.

@AraujoJordan
Created May 28, 2015 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AraujoJordan/7555f67bb05631723731 to your computer and use it in GitHub Desktop.
Save AraujoJordan/7555f67bb05631723731 to your computer and use it in GitHub Desktop.
Desafio 2
/**
* Create a new image file and save it to Drive. (Extract the Bitmap from imageview)
*/
private void saveImageToDrive(ImageView iv) {
final Bitmap image = drawableToBitmap(iv.getDrawable());
Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
return;
}
OutputStream outputStream = result.getDriveContents().getOutputStream();
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e1) {
}
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("image/png").setTitle("Expotec.png").build();
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(mGoogleApiClient);
try {
startIntentSenderForResult(
intentSender, 111, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
}
}
});
}
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment