代码之家  ›  专栏  ›  技术社区  ›  navylover

为什么读取内部存储中的文件需要权限?

  •  1
  • navylover  · 技术社区  · 8 年前

    以下是阅读代码:

            try {
              FileInputStream fis = openFileInput("img.png"); 
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    

    从设备浏览器,我确保img。png文件确实存在: enter image description here

    但出现了错误,这是logcat:

    /data/data/xxx.xxx.xxx/files/img.png: open failed: EACCES (Permission denied)
    

    正如官方医生所说,

    与外部存储目录不同,你的应用不需要 读取和写入内部目录的任何系统权限 通过这些方法返回

    为什么会这样? .

    2 回复  |  直到 8 年前
        1
  •  2
  •   Akash Mishra    8 年前

    我知道了,你正在尝试添加应用程序内部目录中的文件。 我创建了一个应用程序,可以在文件目录中生成一个随机位图图像并共享。您可以使用此代码创建解决方案。

    最初,您需要在“res”文件夹下的XML目录中创建一个XML文件。这是我项目的完整结构。

    directories

    将此代码粘贴到文件_provider中。xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <files-path
            name="files"
            path="." />
    </paths>
    

    现在在AndroidManifest中添加提供者。标记之间的xml。

    <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.myfileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider" />
        </provider>
    

    在这次转移到主要活动之后。java文件。我还使用了一个java类位图工厂来创建一个随机位图。我想你不需要。

    public class MainActivity extends Activity {
    
        private static final String SHARED_PROVIDER_AUTHORITY = BuildConfig.APPLICATION_ID + ".myfileprovider";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void share(final View view) throws IOException {
            // Create a random image and save it in private app folder
            final File sharedFile = createFile();
    
            // Get the shared file's Uri
            final Uri uri = FileProvider.getUriForFile(this, SHARED_PROVIDER_AUTHORITY, sharedFile);
    
            // Create a intent
            final ShareCompat.IntentBuilder intentBuilder = ShareCompat.IntentBuilder.from(this)
                .setType("image/*")
                .addStream(uri);
    
            // Start the intent
            final Intent chooserIntent = intentBuilder.createChooserIntent();
            startActivity(chooserIntent);
        }
    
        @NonNull
        private File createFile() throws IOException {
            final RandomBitmapFactory bitmapFactory = new RandomBitmapFactory();
            final Bitmap randomBitmap = bitmapFactory.createRandomBitmap();
    
            final File sharedFile = File.createTempFile("picture", ".png", getFilesDir());
            sharedFile.createNewFile();
    
            writeBitmap(sharedFile, randomBitmap);
            return sharedFile;
        }
    
        private static void writeBitmap(final File destination,
                                    final Bitmap bitmap) {
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(destination);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                close(outputStream);
            }
        }
    
        private static void close(final Closeable closeable) {
            if (closeable == null) return;
            try {
                closeable.close();
            } catch (IOException ignored) {
            }
        }
    }
    

    这是一个完整的程序,可以在没有任何权限的情况下将图像文件读写到内部目录。

    Image saved in internal

    希望这能解决你的问题。

        2
  •  -2
  •   kumud kala    8 年前

    按程序检查权限。当你想访问该文件时。

    int permissionCheck = ContextCompat.checkSelfPermission(ctx,READ_EXTERNAL_STORAGE);
    if (permissionCheck== PackageManager.PERMISSION_GRANTED){
     //this means permission is granted and you can do read and write
    }else{
    ActivityCompat.requestPermissions((Activity) ctx, 
    

    新字符串[{Manifest.permission.READ_EXTERNAL_STORAGE},1); }