我正在制作一个可从相机捕获图像并将其上传到Firebase的应用程序。将图像上传到Firebase之后,我想从图库的Camera文件夹和另一个名为Pictures的文件夹中删除该图像,该文件夹在捕获图像后自动创建。我尝试了SO的多种解决方案,但没有一个可行。我试图使用file.delete()和路径从uri中删除图像,但是图像仍在上述文件夹中。我正在使用API 21,因此我在某处读过一些内容,我们无法通过file.delete()访问sd卡文件,那么有什么解决方案?请提出在> = API 19的每台设备上都可以使用的方法。另外,无论在何处保存图像(即是外部存储器还是内部存储器),都建议一种可行的方法,因为我不知道存储设置用户可以在手机上使用。
我在这里提供一些代码段,请告诉我是否还有其他需要。
我正在创建这样的意图对象:
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的各种答案中删除了我尝试过的所有不需要的代码,这就是原始代码。
答案 0 :(得分:0)
只需创建您自己的文件以在任何目录中捕获,然后将file_path(String)保存在任何位置即可。
然后将该文件传递给意图
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);
从file_path获取URI或文件,并从文件路径获取所需的图像或位图。 按文件路径删除文件。
请检查此文件提供程序
https://developer.android.com/reference/android/support/v4/content/FileProvider