Skip to content

Instantly share code, notes, and snippets.

@Guilherme-HRamos
Created July 25, 2018 00:00
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 Guilherme-HRamos/374f984aa41108fa3b6ab4896290ccfe to your computer and use it in GitHub Desktop.
Save Guilherme-HRamos/374f984aa41108fa3b6ab4896290ccfe to your computer and use it in GitHub Desktop.
Obtenção de Bitmaps
public class BitmapUtils extends AsyncTask<Uri, Void, Bitmap>{
private final Context mContext;
private BuildBitmapCallback mCallback;
private boolean isFinished = false;
private boolean needShownMessage = true;
public BitmapUtils(final Context context, final BuildBitmapCallback callback) {
mContext = context;
mCallback = callback;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mCallback.onUpdateProgress(null);
onVerifyProgress();
}
@Override
protected Bitmap doInBackground(final Uri... uri) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
if (mContext != null) {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), uri[0]);
bitmap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
return bitmap;
} else {
if (mCallback != null)
mCallback.onUpdateProgress("Não foi possível construir a imagem. Erro: Contexto nulo");
return null;
}
} catch (IOException e) {
e.printStackTrace();
if (mCallback != null)
mCallback.onUpdateProgress("Não foi possível construir a imagem. Erro: " + e.getCause());
return null;
} finally {
try {
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
protected void onPostExecute(final Bitmap result) {
super.onPostExecute(result);
if (mCallback != null)
mCallback.onFinished(result);
isFinished = true;
mCallback = null;
}
private void onVerifyProgress() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!isFinished && needShownMessage) {
needShownMessage = false;
if (mCallback != null)
mCallback.onUpdateProgress("Melhorando imagem...");
}
}
}, 3000);
}
}
public interface BuildBitmapCallback {
void onUpdateProgress(@Nullable String message);
void onFinished(Bitmap bitmap);
}
public class ExampleActivity extends AppCompatActivity {
private static final int TYPE_ONE = 13;
private static final int TYPE_TWO = 8;
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
new BitmapUtils(getContext(), new BuildBitmapCallback() {
@Override
public void onUpdateProgress(String message) {
if (message == null)
showProgress();
else
showProgress(message);
}
@Override
public void onFinished(final Bitmap bitmap) {
if (bitmap == null)
return;
if (requestCode == TYPE_ONE) {
//...
} else if (requestCode == TYPE_TWO) {
//...
}
hideProgress();
}
}).execute(data.getData());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment