问题描述
我知道有人以多种不同的方式询问过这个问题,但我似乎仍然无法从默认文件夹中删除图库图像.我正在将文件正确保存到 SD 卡,我可以很好地删除该文件,但显示在文件夹 Camera 下的默认图库图像文件不会删除.
I know this has been asked in many different ways but I still can not seem to delete the gallery image from the default folder. I'm saving the file to the SD card correctly and I can delete that file fine, but the default gallery image file that shows under the folder Camera will not delete.
我希望在活动返回后删除图像,因为文件已经存储在 SD 卡上 /Coupon2
下.
I would like the image to delete once the activity is returned since the file is already stored on the SD card under /Coupon2
.
有什么建议吗?
public void startCamera() {
Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
mManufacturerText = (EditText) findViewById(R.id.manufacturer);
String ManufacturerText = mManufacturerText.getText().toString();
String currentDateTimeString = new Date().toString();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File filedir = new File(Environment.getExternalStorageDirectory()+"/Coupon2");
filedir.mkdirs();
File file = new File(Environment.getExternalStorageDirectory()+"/Coupon2", ManufacturerText+"-test.png");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == -1) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.putExtra("crop", "true");
intent.putExtra("scale", "true");
intent.putExtra("return-data", false);
intent.setDataAndType(outputFileUri, "image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_CROP_REQUEST);
}else {
SetImage();
saveState();
}
}
推荐答案
我的应用程序要求我调用一个意图来拍照.照片不能在图库中,而必须在 SD 卡上的特定目录中.
My application requires me to call an intent to take a photo. The photo cannot be in the gallery but instead must be in a specific directory on the SD card.
最初我只是使用了 EXTRA_OUTPUT,但我很快发现了以下内容:- 某些设备完全使用它并跳过图库.- 一些设备完全忽略它,只使用图库.- 一些设备真的很糟糕,将全尺寸图像保存到图库中,并且只将缩略图保存到我想要的位置.(HTC 你知道你是谁...)
Originally I just used the EXTRA_OUTPUT, but I soon discovered the following: - Some devices use it completely and skip the gallery. - Some devices ignore it completely and ONLY use the gallery. - Some devices really suck and save a full sized image to the gallery, and save a thumbnail only to the location I wanted. (HTC you know who you are...)
所以,我不能在完成后盲目地删除图库文件.最后添加的照片可能是也可能不是我想要删除的照片.此外,我可能必须复制该文件以替换我自己的文件.因为我的活动是 2000 行,而且我的公司不希望发布我们所有的代码,所以我只发布了执行此操作所涉及的方法.希望这会有所帮助.
So, I can't blindly delete a gallery file when I'm done. The last added photo may or may not be the one I want to remove. Also, I may have to copy that file replacing my own afterwards. Because my activity is 2000 lines, and my company wouldn't want all of our code being posted, I'm posting only the methods involved in doing this. Hopefully this helps.
另外,我要声明,这是我的第一个 Android 应用程序.如果有我不知道的更好的方法来做到这一点,我不会感到惊讶,但这对我有用!
Also, I'll state, this is my first Android application. I wouldn't be surprised if there was a better way to do this that I just don't know about, but this is what's working for me!
所以,这是我的解决方案:
So, here is my solution:
首先,在我的应用程序上下文中,我定义了一个变量,如下所示:
First, in my application context I define a variable as follows:
public ArrayList<String> GalleryList = new ArrayList<String>();
接下来,在我的活动中,我定义了一个方法来获取图库中所有照片的列表:
Next, in my activity, I define a method to get a list of all photos in the gallery:
private void FillPhotoList()
{
// initialize the list!
app.GalleryList.clear();
String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME };
// intialize the Uri and the Cursor, and the current expected size.
Cursor c = null;
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
//
// Query the Uri to get the data path. Only if the Uri is valid.
if (u != null)
{
c = managedQuery(u, projection, null, null, null);
}
// If we found the cursor and found a record in it (we also have the id).
if ((c != null) && (c.moveToFirst()))
{
do
{
// Loop each and add to the list.
app.GalleryList.add(c.getString(0));
}
while (c.moveToNext());
}
}
这是为我的新图像返回唯一文件名的方法:
Here's a method to return a unique file name for my new image:
private String getTempFileString()
{
// Only one time will we grab this location.
final File path = new File(Environment.getExternalStorageDirectory(),
getString(getApplicationInfo().labelRes));
//
// If this does not exist, we can create it here.
if (!path.exists())
{
path.mkdir();
}
//
return new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg").getPath();
}
我的 Activity 中有三个变量,用于为我存储有关当前文件的信息.字符串(路径)、文件变量和该文件的 URI:
I have three variables in my Activity that store information for me about a current file. A string (path), a File variable, and a URI to that file:
public static String sFilePath = "";
public static File CurrentFile = null;
public static Uri CurrentUri = null;
我从不直接设置这些,我只在文件路径上调用一个setter:
I never set these directly, I only call a setter on the file path:
public void setsFilePath(String value)
{
// We just updated this value. Set the property first.
sFilePath = value;
//
// initialize these two
CurrentFile = null;
CurrentUri = null;
//
// If we have something real, setup the file and the Uri.
if (!sFilePath.equalsIgnoreCase(""))
{
CurrentFile = new File(sFilePath);
CurrentUri = Uri.fromFile(CurrentFile);
}
}
现在我调用了一个拍照的意图.
Now I call an intent to take a photo.
public void startCamera()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Specify the output. This will be unique.
setsFilePath(getTempFileString());
//
intent.putExtra(MediaStore.EXTRA_OUTPUT, CurrentUri);
//
// Keep a list for afterwards
FillPhotoList();
//
// finally start the intent and wait for a result.
startActivityForResult(intent, IMAGE_CAPTURE);
}
一旦完成,活动又回来了,这是我的代码:
Once this is done, and the activity comes back, here is my code:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == IMAGE_CAPTURE)
{
// based on the result we either set the preview or show a quick toast splash.
if (resultCode == RESULT_OK)
{
// This is ##### ridiculous. Some versions of Android save
// to the MediaStore as well. Not sure why! We don't know what
// name Android will give either, so we get to search for this
// manually and remove it.
String[] projection = { MediaStore.Images.ImageColumns.SIZE,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATA,
BaseColumns._ID,};
//
// intialize the Uri and the Cursor, and the current expected size.
Cursor c = null;
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
//
if (CurrentFile != null)
{
// Query the Uri to get the data path. Only if the Uri is valid,
// and we had a valid size to be searching for.
if ((u != null) && (CurrentFile.length() > 0))
{
c = managedQuery(u, projection, null, null, null);
}
//
// If we found the cursor and found a record in it (we also have the size).
if ((c != null) && (c.moveToFirst()))
{
do
{
// Check each area in the gallary we built before.
boolean bFound = false;
for (String sGallery : app.GalleryList)
{
if (sGallery.equalsIgnoreCase(c.getString(1)))
{
bFound = true;
break;
}
}
//
// To here we looped the full gallery.
if (!bFound)
{
// This is the NEW image. If the size is bigger, copy it.
// Then delete it!
File f = new File(c.getString(2));
// Ensure it's there, check size, and delete!
if ((f.exists()) && (CurrentFile.length() < c.getLong(0)) && (CurrentFile.delete()))
{
// Finally we can stop the copy.
try
{
CurrentFile.createNewFile();
FileChannel source = null;
FileChannel destination = null;
try
{
source = new FileInputStream(f).getChannel();
destination = new FileOutputStream(CurrentFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally
{
if (source != null)
{
source.close();
}
if (destination != null)
{
destination.close();
}
}
}
catch (IOException e)
{
// Could not copy the file over.
app.CallToast(PhotosActivity.this, getString(R.string.ErrorOccured), 0);
}
}
//
ContentResolver cr = getContentResolver();
cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
BaseColumns._ID + "=" + c.getString(3), null);
break;
}
}
while (c.moveToNext());
}
}
}
}
}
这篇关于拍摄相机意图照片后删除图库图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!