5
void launchImageCapture(Activity context) {
    Uri imageFileUri = context.getContentResolver()
        .insert(Media.INTERNAL_CONTENT_URI, new ContentValues());
    m_queue.add(imageFileUri);
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
    context.startActivityForResult(i, ImportActivity.CAMERA_REQUEST); 
}

上面一直有效的代码现在在 insert() 处为我生成了这个异常。

java.lang.UnsupportedOperationException: Writing to internal storage is not supported.
     at com.android.providers.media.MediaProvider.generateFileName(MediaProvider.java:2336)
     at com.android.providers.media.MediaProvider.ensureFile(MediaProvider.java:1851)
     at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:2006)
     at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1974)
     at android.content.ContentProvider$Transport.insert(ContentProvider.java:150)
     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:140)
     at android.os.Binder.execTransact(Binder.java:287)
     at dalvik.system.NativeStart.run(Native Method)

这不是空间问题,我唯一改变的是一个无关类的包。另外,我重新启动了手机。

4

3 回答 3

12

在这里面临同样的问题,我很高兴找到这个线程。尽管在这个解决方法中有两件事困扰着我,但这篇文章让我找到了正确的方向。我想分享我自己的解决方法/解决方案。

让我首先说明我没有看到自己生活的东西。

首先,我不想将应用程序私有文件保留为 MODE_WORLD_WRITEABLE。这对我来说似乎是无意义的,尽管我无法确切地知道另一个应用程序如何访问这个文件,除非知道在哪里可以找到完整的名称和路径。我并不是说这对您的情况一定不好,但它仍然以某种方式困扰着我。我宁愿通过让图片文件对我的应用程序真正私有来覆盖我的所有基础。在我的商业案例中,图片在应用程序之外没有任何用处,并且绝不应该通过 Android Gallery 等方式删除它们。我的应用程序将在适当的时间触发清理,以免吸食 Droid 设备的存储空间。

其次,openFileOutput()没有留下任何选项,而是将生成的文件保存在getFilesDir()的根目录中。如果我需要一些目录结构来保持井井有条怎么办?此外,我的应用程序必须处理多张图片,因此我希望生成文件名,以便以后参考。

看,很容易用相机拍摄照片并将其保存到 Droid 设备上的公共图像区域(通过 MediaStore)。从 MediaStore 操作(查询、更新、删除)媒体也很容易。有趣的是,将相机图片插入 MediaStore 会生成一个看似唯一的文件名。为具有目录结构的应用程序创建私有文件也很容易。“Capturea 相机图片并将其保存到内部存储器”问题的症结在于您不能直接这样做,因为 Android 阻止 ContentResolver 使用 Media.INTERNAL_CONTENT_URI,并且因为私有应用程序文件根据定义无法通过(外部)访问相机活动。

最后我采用了以下策略:

  1. 启动相机活动以获取我的应用程序的结果,并带有捕获图像的意图。
  2. 返回我的应用程序时,将捕获插入 MediaStore。
  3. 查询MediaStore,获取生成的图片文件名。
  4. 使用Context.getDir()在相​​对于私有应用程序数据文件夹的任何路径上创建一个真正的内部文件。
  5. 使用 OutputStream 将位图数据写入此私有文件。
  6. 从 MediaStore 中删除捕获。
  7. 可选)在我的应用程序中显示捕获的 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 设备生成的。并且公共存储区没有任何东西保存,从而防止了意外的图片篡改。

于 2011-10-19T15:36:11.937 回答
5

这是我将捕获的图像从相机保存到应用程序内部存储的工作代码:

首先,创建具有所需文件名的文件。在这种情况下,它是“MyFile.jpg”,然后按照以下意图启动活动。你是回调方法(onActivityResult),一旦完成将被调用。调用 onActivityResult 后,您的图像应保存到内部存储中。关键说明:使用的模式openFileOutput需要是全局的。Context.MODE_WORLD_WRITEABLE工作正常,我没有测试过其他模式。

try {
FileOutputStream fos = openFileOutput("MyFile.jpg", Context.MODE_WORLD_WRITEABLE);
fos.close();
File f = new File(getFilesDir() + File.separator + "MyFile.jpg");
startActivityForResult(
        new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            .putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f))
        , IMAGE_CAPTURE_REQUEST_CODE);
}
catch(IOException e) {

}

在活动结果方法中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == IMAGE_CAPTURE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        Log.i(TAG, "Image is saved.");
    }
}

检索您的图像:

try {
InputStream is = openFileInput("MyFile.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 4;
Bitmap retrievedBitmap = BitmapFactory.decodeStream(is, null, options);
}
catch(IOException e) {

}
于 2011-03-09T21:42:59.590 回答
2

相机显然不支持写入内部存储。

不幸的是,文档中没有提到这一点。

MediaProvider.java有以下代码:

private String generateFileName(boolean internal,
    String preferredExtension, String directoryName)
{
     // create a random file
    String name = String.valueOf(System.currentTimeMillis());

    if (internal) {
        throw new UnsupportedOperationException(
            "Writing to internal storage is not supported.");
//      return Environment.getDataDirectory()
//          + "/" + directoryName + "/" + name + preferredExtension;
    } else {
        return Environment.getExternalStorageDirectory()
            + "/" + directoryName + "/" + name + preferredExtension;
    }
}

因此暂时故意禁用写入内部存储。

编辑- 我认为您可以使用 binnyb 的方法作为解决方法,但我不推荐它;我不确定这是否会继续适用于未来的版本。我认为其目的是禁止写入媒体文件的内部存储。

我在 Android 问题跟踪器中提交了一个错误。

编辑- 我现在明白为什么 binnyb 的方法有效。相机应用程序被认为只是另一个应用程序。如果没有权限,则无法写入内部存储。将您的文件设置为全局可写会授予其他应用程序写入该文件的权限。

但是,我仍然认为这不是一个好主意,原因如下:

  • 您通常不希望其他应用程序写入您的私人存储。
  • 某些手机​​的内部存储空间非常有限,原始相机图像非常大。
  • 如果您仍然打算调整图像大小,那么您可以从外部存储中读取它,然后自己将其写入内部存储。
于 2011-03-09T21:20:45.813 回答