Trouble writing internal memory android

后端 未结 3 2105
有刺的猬
有刺的猬 2020-12-30 16:04
void launchImageCapture(Activity context) {
    Uri imageFileUri = context.getContentResolver()
        .insert(Media.INTERNAL_CONTENT_URI, new ContentValues());
            


        
3条回答
  •  心在旅途
    2020-12-30 17:05

    The camera apparently doesn't support writing to internal storage.

    Unfortunately this is not mentioned in the documentation.

    MediaProvider.java has the following code:

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

    So writing to internal storage has been intentionally disabled for the time being.

    Edit - I think you can use binnyb's method as a work-around, but I wouldn't recommend it; I'm not sure if this will continue to work on future versions. I think the intention is to disallow writing to internal storage for media files.

    I filed a bug in the Android issue tracker.

    Edit - I now understand why binnyb's method works. The camera app is considered to be just another application. It can't write to internal storage if it doesn't have permissions. Setting your file to be world-writable gives other applications permission to write to that file.

    I still don't think that this is a very good idea, however, for a few reasons:

    • You don't generally want other apps writing to your private storage.
    • Internal storage is quite limited on some phones, and raw camera images are quite large.
    • If you were planning on resizing the image anyway, then you can read it from external storage and write it yourself to your internal storage.

提交回复
热议问题