麻烦编写内部内存android

时间:2011-03-09 21:10:23

标签: android exception storage android-contentprovider android-contentresolver

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)

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

3 个答案:

答案 0 :(得分:11)

在这里遇到同样的问题,我很高兴找到这个帖子。尽管在这个变通方法中有两件事让我烦恼,但这篇文章让我看向了正确的方向。我想分享我自己的解决方法/解决方案。

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

首先,我不想将应用程序私有文件保留为MODE_WORLD_WRITEABLE。这看起来对我来说没有意义,虽然我无法确定完全另一个应用程序如何访问此文件,除非知道在哪里查找具有完整名称和路径的文件。我并不是说它对你的场景来说一定不好,但它仍然以某种方式困扰着我。我希望通过将图片文件专用于我的应用程序来覆盖我的所有基础。在我的商业案例中,图片在应用程序之外是没有用的,绝不应该通过Android Gallery删除它们。我的应用程序将在适当的时间触发清理,以便不吸收Droid设备存储空间。

其次, openFileOutput()不保留任何选项,只是将结果文件保存在 getFilesDir()的根目录中。如果我需要一些目录结构来保持秩序怎么办?另外,我的应用程序必须处理多个图片,所以我想生成文件名,以便稍后可以参考。

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

最后我采用了以下策略:

  1. 从我的应用程序启动Camera活动以获取结果,并使用Intent捕获图像。
  2. 返回我的应用时,将捕获插入MediaStore。
  3. 查询MediaStore以获取生成的图像文件名。
  4. 使用 Context.getDir()在相对于私有应用程序数据文件夹的任何路径上创建一个真正的内部文件。
  5. 使用OutputStream将位图数据写入此私有文件。
  6. 从MediaStore删除捕获。
  7. 可选)在我的应用中显示捕获的ImageView。
  8. 以下是启动cam的代码:

    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);
    }
    

    这是我的活动获得凸轮结果:

    @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设备生成。公共存储区域没有任何东西,因此可以防止意外的图片处理。

答案 1 :(得分: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) {

}

答案 2 :(得分: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问题跟踪器中提交了bug

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

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

  • 您通常不希望其他应用写入您的私人存储空间。
  • 某些手机的内部存储空间非常有限,原始相机图像非常大。
  • 如果您打算重新调整图像大小,那么您可以从外部存储中读取它并将其自己写入内部存储。