Created
May 28, 2015 06:49
-
-
Save AraujoJordan/7555f67bb05631723731 to your computer and use it in GitHub Desktop.
Desafio 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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