Skip to content

Instantly share code, notes, and snippets.

@aivision369
Created August 1, 2018 09:57
Show Gist options
  • Save aivision369/3639a6811b441ff3f75406e073915053 to your computer and use it in GitHub Desktop.
Save aivision369/3639a6811b441ff3f75406e073915053 to your computer and use it in GitHub Desktop.
public class NewActivity extends AppCompatActivity {
private static final String TAG = "[NewActivity]";
private static final int PHOTO_REQUEST_CODE = 102;
private String path;
Uri photoURI = null;
@BindView(R.id.img_output)
ImageView imgOutput;
@BindView(R.id.img_camera)
ImageView imgCamera;
@BindView(R.id.btn_delete)
Button btnDelete;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
final PermissionManager permissionManager = new PermissionManager();
Glide.with(this).load(R.drawable.photo).into(imgCamera);
imgCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (permissionManager.userHasPermission(NewActivity.this)) {
takePicture();
} else {
permissionManager.requestPermission(NewActivity.this);
}
}
});
btnDelete.setOnClickListener(v -> {
File fi = new File(path);
deleteAndScanFile(this, path, fi, photoURI);
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Bitmap source = BitmapFactory.decodeFile(path, provideCompressionBitmapFactoryOptions());
imgOutput.setImageBitmap(source);
}
}
private void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
try {
File photoFile = createImageFileWith();
path = photoFile.getAbsolutePath();
photoURI = FileProvider.getUriForFile(NewActivity.this,
getString(R.string.file_provider_authority),
photoFile);
} catch (IOException ex) {
Log.e("TakePicture", ex.getMessage());
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
takePictureIntent.setClipData(ClipData.newRawUri("", photoURI));
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
startActivityForResult(takePictureIntent, PHOTO_REQUEST_CODE);
}
}
private static BitmapFactory.Options provideCompressionBitmapFactoryOptions() {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = false;
opt.inPreferredConfig = Bitmap.Config.RGB_565;
return opt;
}
public File createImageFileWith() throws IOException {
final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
final String imageFileName = "JPEG_" + timestamp;
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pics");
storageDir.mkdirs();
return File.createTempFile(imageFileName, ".jpg", storageDir);
}
private void deleteAndScanFile(final Context context, String path,
final File fi, Uri photoURI) {
String fpath = path.substring(path.lastIndexOf("/") + 1);
Log.i("fpath", fpath);
try {
MediaScannerConnection.scanFile(context, new String[]{Environment
.getExternalStorageDirectory().toString()
+ "/images/"
+ fpath.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
if (photoURI != null) {
context.getContentResolver().delete(photoURI, null,
null);//here is the line where i am getting exception
}
fi.delete();
System.out.println("file Deleted :" + fi.getPath());
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment