我的应用程序需要调用一个意图来拍照。照片不能在相册中,而必须在SD卡上的特定目录中。
最初我只是使用了EXTRA_OUTPUT,但很快发现以下问题:
- 一些设备完全使用它并跳过相册。
- 一些设备完全忽略它,并且仅使用相册。
- 一些设备真的很差,将完整大小的图像保存到相册中,并只将缩略图保存到我想要的位置。(HTC你知道你是谁...)
所以,我不能盲目地删除相册文件。最后添加的照片可能是我想要删除的,也可能不是。此外,我可能需要复制该文件以替换自己的文件。因为我的活动有2000行,我的公司不希望我们的所有代码都被发布,所以我只发布涉及此操作的方法。希望这能有所帮助。
另外,我要声明,这是我第一个Android应用程序。如果有更好的方法可以做到这一点,我不会感到惊讶,但这是对我有效的方法!
所以,这是我的解决方案:
首先,在我的应用程序上下文中,我定义一个变量如下:
public ArrayList<String> GalleryList = new ArrayList<String>();
接下来,在我的活动中,我定义了一个方法来获取相册中所有照片的列表:
private void FillPhotoList()
{
app.GalleryList.clear();
String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME };
Cursor c = null;
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (u != null)
{
c = managedQuery(u, projection, null, null, null);
}
if ((c != null) && (c.moveToFirst()))
{
do
{
app.GalleryList.add(c.getString(0));
}
while (c.moveToNext());
}
}
这里提供一种方法来返回我新图片的唯一文件名:
private String getTempFileString()
{
final File path = new File(Environment.getExternalStorageDirectory(),
getString(getApplicationInfo().labelRes));
if (!path.exists())
{
path.mkdir();
}
return new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg").getPath();
}
我在Activity中有三个变量,用于存储当前文件的信息。一个字符串(路径),一个File变量和该文件的URI:
public static String sFilePath = "";
public static File CurrentFile = null;
public static Uri CurrentUri = null;
我从未直接设置这些内容,只是在文件路径上调用了一个 setter:
public void setsFilePath(String value)
{
sFilePath = value;
CurrentFile = null;
CurrentUri = null;
if (!sFilePath.equalsIgnoreCase(""))
{
CurrentFile = new File(sFilePath);
CurrentUri = Uri.fromFile(CurrentFile);
}
}
现在我调用一个Intent来拍照。
public void startCamera()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
setsFilePath(getTempFileString());
intent.putExtra(MediaStore.EXTRA_OUTPUT, CurrentUri);
FillPhotoList();
startActivityForResult(intent, IMAGE_CAPTURE);
}
完成此操作后,当活动返回时,以下是我的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == IMAGE_CAPTURE)
{
if (resultCode == RESULT_OK)
{
String[] projection = { MediaStore.Images.ImageColumns.SIZE,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATA,
BaseColumns._ID,};
Cursor c = null;
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (CurrentFile != null)
{
if ((u != null) && (CurrentFile.length() > 0))
{
c = managedQuery(u, projection, null, null, null);
}
if ((c != null) && (c.moveToFirst()))
{
do
{
boolean bFound = false;
for (String sGallery : app.GalleryList)
{
if (sGallery.equalsIgnoreCase(c.getString(1)))
{
bFound = true;
break;
}
}
if (!bFound)
{
File f = new File(c.getString(2));
if ((f.exists()) && (CurrentFile.length() < c.getLong(0)) && (CurrentFile.delete()))
{
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)
{
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());
}
}
}
}
}