Last active
July 18, 2019 15:29
-
-
Save ronnieotieno/59d3404b607ffb10bbc20a03bbc5245a to your computer and use it in GitHub Desktop.
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
public class BackgroundImageResize extends AsyncTask<Uri, Integer, byte[]> { | |
Bitmap mBitmap; | |
byte[] mUploadBytes; | |
Context context; | |
public interface OnDataSendToActivity { | |
public void sendData(byte[] bytes); | |
} | |
public static OnDataSendToActivity dataSendToActivity; | |
public BackgroundImageResize(Context ctx) { | |
context = ctx.getApplicationContext(); | |
} | |
public BackgroundImageResize(Bitmap bitmap) { | |
if (bitmap != null) { | |
this.mBitmap = bitmap; | |
} | |
} | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
Toast.makeText(context, "Compressing the Image", Toast.LENGTH_SHORT).show(); | |
progressBar.setVisibility(View.VISIBLE); | |
try { | |
dataSendToActivity = (OnDataSendToActivity) BackgroundImageResize.this; | |
} catch (ClassCastException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
protected byte[] doInBackground(Uri... params) { | |
try { | |
mBitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), params[0]); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
byte[] bytes; | |
bytes = getBytesFromBitmap(mBitmap, 70); | |
return bytes; | |
} | |
@Override | |
protected void onPostExecute(byte[] bytes) { | |
super.onPostExecute(bytes); | |
mUploadBytes = bytes; | |
progressBar.setVisibility(View.INVISIBLE); | |
//execute the upload task | |
ChooseImageActivity.mOnInputListener.sendInput(mUploadBytes); | |
dataSendToActivity.sendData(mUploadBytes); | |
} | |
public static byte[] getBytesFromBitmap(Bitmap bitmap, int quality) { | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream); | |
return stream.toByteArray(); | |
} | |
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
public class ChooseImageActivity extends DialogFragment implements BackgroundImageResize.OnDataSendToActivity { | |
TextView textViewChoose, textViewTake; | |
Uri uriProfileImage2; | |
public static ProgressBar progressBar; | |
@Override | |
public void sendData(byte[] bytes) { | |
getDialog().dismiss(); | |
} | |
public interface OnInputListener { | |
void sendInput(byte[] uriProfileImage2); | |
} | |
public static OnInputListener mOnInputListener; | |
@Nullable | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.activity_choose_image, container, false); | |
textViewChoose = view.findViewById(R.id.textViewChoose); | |
textViewTake = view.findViewById(R.id.textViewPhoto); | |
progressBar = view.findViewById(R.id.progress_bar_Dialog); | |
textViewTake.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (ContextCompat.checkSelfPermission(getActivity(), | |
Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { | |
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE"); | |
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); | |
} else { | |
requestCameraPermission(); | |
} | |
} | |
}); | |
textViewChoose.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (ContextCompat.checkSelfPermission(getActivity(), | |
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { | |
Intent intent = new Intent(); | |
intent.setType("image/*"); | |
intent.setAction(Intent.ACTION_GET_CONTENT); | |
startActivityForResult(Intent.createChooser(intent, "Select Profile Image"), STORAGE_REQUEST); | |
} else { | |
requestStoragePermission(); | |
} | |
} | |
}); | |
return view; | |
} | |
private void requestStoragePermission() { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), | |
Manifest.permission.READ_EXTERNAL_STORAGE)) { | |
new AlertDialog.Builder(getActivity()) | |
.setTitle("Permission needed") | |
.setMessage("This permission is needed inorder to update Profile Picture") | |
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
ActivityCompat.requestPermissions(getActivity(), | |
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); | |
} | |
}) | |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
} | |
}) | |
.create().show(); | |
} else { | |
ActivityCompat.requestPermissions(getActivity(), | |
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); | |
} | |
} | |
private void requestCameraPermission() { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), | |
Manifest.permission.CAMERA)) { | |
new AlertDialog.Builder(getActivity()) | |
.setTitle("Permission needed") | |
.setMessage("This permission is needed inorder to capture profile picture") | |
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
ActivityCompat.requestPermissions(getActivity(), | |
new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION); | |
} | |
}) | |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
} | |
}) | |
.create().show(); | |
} else { | |
ActivityCompat.requestPermissions(getActivity(), | |
new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
if (requestCode == CAMERA_PERMISSION) { | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE"); | |
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); | |
} else { | |
Toast.makeText(getActivity(), "Permission DENIED", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { | |
uriProfileImage2 = data.getData(); | |
BackgroundImageResize backgroundImageResize = new BackgroundImageResize(getActivity()); | |
backgroundImageResize.execute(uriProfileImage2); | |
} else if (requestCode == STORAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { | |
uriProfileImage2 = data.getData(); | |
BackgroundImageResize backgroundImageResize = new BackgroundImageResize(getActivity()); | |
backgroundImageResize.execute(uriProfileImage2); | |
} | |
} | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
try { | |
mOnInputListener = (OnInputListener) getActivity(); | |
} catch (ClassCastException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment