Reputation: 131
I'm making an app that captures image from camera and uploades it to firebase. After uploading the image to firebase, I want to remove the image from the Camera folder of gallery and another folder named Pictures which is created automatically after an image is captured. I have tried multiple solutions from SO, but none worked. I have tried to remove image from uri using file.delete() and from path, but the image is still there in the above mentioned folders. I'm using API 21, and I have read on SO somewhere that we cannot access sd card files by file.delete(), so what could be a solution? Please suggest something that works on every device >= API 19. Also, please suggest a way that works no matter where the image is being saved, i.e. whether it is external or internal memory, because I don't know about the storage settings a user would have on his phone.
I am providing some code snippets here, please let me know if anything else is needed.
I'm creating intent object like this:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
I'm doing the uploading work here:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK && data!= null){
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.waitwhilepicisuploaded);
mediaPlayer.start();
final Bitmap photo = (Bitmap) data.getExtras().get("data");
//the tap to open camera button disappears
tapCameraBtn.setVisibility(Button.GONE);
//setting the color of progress bar to white
progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(this, android.R.color.white), PorterDuff.Mode.SRC_IN );
//and now we make the progress bar visible instead of the button
progressBar.setVisibility(ProgressBar.VISIBLE);
mCount = FirebaseDatabase.getInstance().getReference().child(FirebaseAuth.getInstance().getCurrentUser().getPhoneNumber().toString()).child("count");
uploadPhoto(mCount, photo);
}
}
public void uploadPhoto(DatabaseReference mCount, Bitmap photo){
final Uri uri = getImageUri(getApplicationContext(), photo);
final String userPhoneNumber = FirebaseAuth.getInstance().getCurrentUser().getPhoneNumber();
uniquefilename = userPhoneNumber.toString();
mCount.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
photoCounter = dataSnapshot.getValue(Integer.class);
//uploading image captured to firebase
uploadPhotoToFirebase(uri, userPhoneNumber, uniquefilename, photoCounter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("The read failed: ", "FAILED");
}
});
}
public void uploadPhotoToFirebase(Uri uri, final String userPhoneNumber, String uniquefilename, int photoCounter){
final StorageReference filepath = storageReference.child("/" + uniquefilename + "/photos/" + "photo_" + photoCounter);
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mediaPlayer.stop();
mediaPlayer.release();
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Toast.makeText(getApplicationContext(), uri.toString(), Toast.LENGTH_SHORT).show();
deleteFile(uri);
uploadPhotoToKairos(uri,userPhoneNumber);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(getApplicationContext(), "URI not found", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(AddContactActivity.this, "Uploading finished!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(AddContactActivity.this, RecordAudioActivity.class);
startActivity(intent);
finish();
}
});
}
deleteFile(Uri uri){
File fdelete = new File(uri.getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.d("file deleted" , uri.getPath());
} else {
Log.d("file not Deleted " , uri.getPath());
}
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
Also, the following permissions are there in manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
I have removed all the unwanted code that I tried from various answers from SO, and this is the original code.
Upvotes: 3
Views: 2719
Reputation: 91
Just create your own file for capturing in any of the directory and save that file_path(String) anywhere.
then pass that file to intent like this
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
//for nougat and above using file provider..
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(this, "provider path(like package)", file));
} else {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
}
startActivityForResult(takePictureIntent,
CAMERA_IMAGE_REQUEST);
get the URI or file from file_path and get the image or bitmap whatever you need from file path. delete the file by file path.
please check this for file provider
https://developer.android.com/reference/android/support/v4/content/FileProvider
Upvotes: 1