android - 无法写入内部存储器android

标签 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)

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

最佳答案

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

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

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

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

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

最后我采用了以下策略:

  1. 为我的应用程序的结果启动相机 Activity ,目的是捕捉图像。
  2. 返回到我的应用程序时,将捕获插入到 MediaStore。
  3. 查询MediaStore获取生成的图片文件名。
  4. 使用 Context.getDir() 在相对于私有(private)应用程序数据文件夹的任何路径上创建一个真正的内部文件。
  5. 使用 OutputStream 将位图数据写入此私有(private)文件。
  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);
}

这是我获取摄像头结果的 Activity :

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

瞧!现在我们有了一个真正的应用程序私有(private)图片文件,这个文件的名称已经由Droid 设备生成。并且公共(public)存储区没有任何内容,从而防止意外的图片操作。

关于android - 无法写入内部存储器android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5252193/

相关文章:

Android - 跟踪线程

c - Lua设置默认错误处理程序

linux - 在 Linux 中测试 IO 性能

docker - 如何在启动时在docker中设置标志

jquery - 使用本地/ session 存储保存 .addClass()

android - 合并同一应用程序的多个推送消息

android - 尝试在 Android Studio 上运行单元测试时出现 NoClassDefFoundError

java - 在安卓模拟器中访问谷歌地图

java - Stripes 异常处理程序错误

java - 当一个正在执行的线程中发生 stackoverflow 异常时,JVM 是否退出?