Skip to content

Instantly share code, notes, and snippets.

@esabook
Created May 15, 2019 15:34
Show Gist options
  • Save esabook/478aa004dbd98cbf8a3c8e0adc35cc4c to your computer and use it in GitHub Desktop.
Save esabook/478aa004dbd98cbf8a3c8e0adc35cc4c to your computer and use it in GitHub Desktop.
access Android local image
@Override
public void onAddAttachments() {
boolean shouldShowReqPermission = !ActivityCompat.shouldShowRequestPermissionRationale(
__GlobalApp.getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE);
boolean isNeedPermission = ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED;
// dialog request permission
if (isNeedPermission) {
if (shouldShowReqPermission) {
// manual storage permission
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setMessage("Enabled read storage permission for continuing.");
alert.setPositiveButton("Open Setting", (dialog, which) -> {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + __GlobalApp.getActivity().getPackageName()));
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
__GlobalApp.getActivity().startActivity(intent);
});
alert.setNegativeButton("Later", (dialog, which) -> dialog.dismiss());
alert.create().show();
} else {
ActivityCompat.requestPermissions(
__GlobalApp.getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
0);
}
} else {
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
String[] mimeTypes = {"image/jpeg", "image/png"};
i.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(i, 361);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 361) {
Uri selectedImage = data.getData();
String customedUri = __ReportImageAttachmentAdapter.TEMP_URI_IMAGE_ATTACHMENT + selectedImage.toString();
if (ImageAttachment.contains(customedUri)) {
Toast.makeText(getActivity(), R.string.please_pick_another_different_image, Toast.LENGTH_LONG).show();
} else {
ImageAttachment.add(customedUri);
ImageAttachmentAdapter.notifyDataSetChanged2();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment