在这里面临同样的问题,我很高兴找到这个线程。尽管在这个解决方法中有两件事困扰着我,但这篇文章让我找到了正确的方向。我想分享我自己的解决方法/解决方案。
让我首先说明我没有看到自己生活的东西。
首先,我不想将应用程序私有文件保留为 MODE_WORLD_WRITEABLE。这对我来说似乎是无意义的,尽管我无法确切地知道另一个应用程序如何访问这个文件,除非知道在哪里可以找到完整的名称和路径。我并不是说这对您的情况一定不好,但它仍然以某种方式困扰着我。我宁愿通过让图片文件对我的应用程序真正私有来覆盖我的所有基础。在我的商业案例中,图片在应用程序之外没有任何用处,并且绝不应该通过 Android Gallery 等方式删除它们。我的应用程序将在适当的时间触发清理,以免吸食 Droid 设备的存储空间。
其次,openFileOutput()没有留下任何选项,而是将生成的文件保存在getFilesDir()的根目录中。如果我需要一些目录结构来保持井井有条怎么办?此外,我的应用程序必须处理多张图片,因此我希望生成文件名,以便以后参考。
看,很容易用相机拍摄照片并将其保存到 Droid 设备上的公共图像区域(通过 MediaStore)。从 MediaStore 操作(查询、更新、删除)媒体也很容易。有趣的是,将相机图片插入 MediaStore 会生成一个看似唯一的文件名。为具有目录结构的应用程序创建私有文件也很容易。“Capturea 相机图片并将其保存到内部存储器”问题的症结在于您不能直接这样做,因为 Android 阻止 ContentResolver 使用 Media.INTERNAL_CONTENT_URI,并且因为私有应用程序文件根据定义无法通过(外部)访问相机活动。
最后我采用了以下策略:
- 启动相机活动以获取我的应用程序的结果,并带有捕获图像的意图。
- 返回我的应用程序时,将捕获插入 MediaStore。
- 查询MediaStore,获取生成的图片文件名。
- 使用Context.getDir()在相对于私有应用程序数据文件夹的任何路径上创建一个真正的内部文件。
- 使用 OutputStream 将位图数据写入此私有文件。
- 从 MediaStore 中删除捕获。
- (可选)在我的应用程序中显示捕获的 ImageView。
这是启动凸轮的代码:
public void onClick (View v)
{
ContentValues values = new ContentValues ();
values.put (Media.IS_PRIVATE, 1);
values.put (Media.TITLE, "Xenios Mobile Private Image");
values.put (Media.DESCRIPTION, "Classification Picture taken via Xenios Mobile.");
Uri picUri = getActivity ().getContentResolver ().insert (Media.EXTERNAL_CONTENT_URI, values);
//Keep a reference in app for now, we might need it later.
((XeniosMob) getActivity ().getApplication ()).setCamPicUri (picUri);
Intent takePicture = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
//May or may not be populated depending on devices.
takePicture.putExtra (MediaStore.EXTRA_OUTPUT, picUri);
getActivity ().startActivityForResult (takePicture, R.id.action_camera_start);
}
这是我获取 cam 结果的活动:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
super.onActivityResult (requestCode, resultCode, data);
if (requestCode == R.id.action_camera_start)
{
if (resultCode == RESULT_OK)
{
Bitmap pic = null;
Uri picUri = null;
//Some Droid devices (as mine: Acer 500 tablet) leave data Intent null.
if (data == null) {
picUri = ((XeniosMob) getApplication ()).getCamPicUri ();
} else
{
Bundle extras = data.getExtras ();
picUri = (Uri) extras.get (MediaStore.EXTRA_OUTPUT);
}
try
{
pic = Media.getBitmap (getContentResolver (), picUri);
} catch (FileNotFoundException ex)
{
Logger.getLogger (getClass ().getName ()).log (Level.SEVERE, null, ex);
} catch (IOException ex)
{
Logger.getLogger (getClass ().getName ()).log (Level.SEVERE, null, ex);
}
//Getting (creating it if necessary) a private directory named app_Pictures
//Using MODE_PRIVATE seems to prefix the directory name provided with "app_".
File dir = getDir (Environment.DIRECTORY_PICTURES, Context.MODE_PRIVATE);
//Query the MediaStore to retrieve generated filename for the capture.
Cursor query = getContentResolver ().query (
picUri,
new String [] {
Media.DISPLAY_NAME,
Media.TITLE
},
null, null, null
);
boolean gotOne = query.moveToFirst ();
File internalFile = null;
if (gotOne)
{
String dn = query.getString (query.getColumnIndexOrThrow (Media.DISPLAY_NAME));
String title = query.getString (query.getColumnIndexOrThrow (Media.TITLE));
query.close ();
//Generated name is a ".jpg" on my device (tablet Acer 500).
//I prefer to work with ".png".
internalFile = new File (dir, dn.subSequence (0, dn.lastIndexOf (".")).toString () + ".png");
internalFile.setReadable (true);
internalFile.setWritable (true);
internalFile.setExecutable (true);
try
{
internalFile.createNewFile ();
//Use an output stream to write picture data to internal file.
FileOutputStream fos = new FileOutputStream (internalFile);
BufferedOutputStream bos = new BufferedOutputStream (fos);
//Use lossless compression.
pic.compress (Bitmap.CompressFormat.PNG, 100, bos);
bos.flush ();
bos.close ();
} catch (FileNotFoundException ex)
{
Logger.getLogger (EvaluationActivity.class.getName()).log (Level.SEVERE, null, ex);
} catch (IOException ex)
{
Logger.getLogger (EvaluationActivity.class.getName()).log (Level.SEVERE, null, ex);
}
}
//Update picture Uri to that of internal file.
((XeniosMob) getApplication ()).setCamPicUri (Uri.fromFile (internalFile));
//Don't keep capture in public storage space (no Android Gallery use)
int delete = getContentResolver ().delete (picUri, null, null);
//rather just keep Uri references here
//visit.add (pic);
//Show the picture in app!
ViewGroup photoLayout = (ViewGroup) findViewById (R.id.layout_photo_area);
ImageView iv = new ImageView (photoLayout.getContext ());
iv.setImageBitmap (pic);
photoLayout.addView (iv, 120, 120);
}
else if (resultCode == RESULT_CANCELED)
{
Toast toast = Toast.makeText (this, "Picture capture has been cancelled.", Toast.LENGTH_LONG);
toast.show ();
}
}
}
瞧!现在我们有了一个真正的应用程序私有图片文件,它的名称是由 Droid 设备生成的。并且公共存储区没有任何东西保存,从而防止了意外的图片篡改。