我正在制作一个从相机中捕获图像并将其上传到Firebase的应用程序。上传图像到Firebase后,我想从“相册”文件夹的相机和另一个名为Pictures的文件夹中删除该图像。在SO上尝试了多个解决方案,但都没有起作用。我尝试使用file.delete()从uri和路径中删除图像,但是仍然可以在上述文件夹中找到该图像。我正在使用API 21,并且在SO上读到过我们无法通过file.delete()访问SD卡文件,那么有什么解决方案吗?请提供一些适用于每个设备>= API 19的解决方案。另外,请提供一种方法,无论图像保存在哪里,即无论是外部还是内部存储器,都可以使用,因为我不知道用户手机上的存储设置。
我在这里提供了一些代码片段,请告诉我是否需要其他内容。
我是这样创建意图对象的:
我正在这里上传文件:
此外,以下权限在清单文件中也存在:
我已经删除了我从SO中试过的所有不需要的代码,这是原始代码。
我在这里提供了一些代码片段,请告诉我是否需要其他内容。
我是这样创建意图对象的:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
我正在这里上传文件:
@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);
}
此外,以下权限在清单文件中也存在:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
我已经删除了我从SO中试过的所有不需要的代码,这是原始代码。
fdelete.exists()
总是返回false
,我也不理解。 - Yuva K