简体   繁体   English

java.io.FileNotFoundException在Android中访问被拒绝

[英]java.io.FileNotFoundException Access is denied in Android

I am trying to store my output file in internal memory.but it throws java.io.FileNotFoundException Access is denied 我正在尝试将输出文件存储在内部存储器中,但是会抛出java.io.FileNotFoundException访问被拒绝

private boolean crop() {
    try {
        FileOutputStream fos = null;
        String filePath = CustomVideoGalleryActivity.videoPath.get(0);
      Movie originalMovie = MovieCreator.build(filePath);

      Track track = originalMovie.getTracks().get(0);
      Movie movie = new Movie();
      movie.addTrack(new AppendTrack(new CroppedTrack(track, 200, 800)));

      Container out = new DefaultMp4Builder().build(movie);
      String outputFilePath = Environment.getDataDirectory()+ "/output_crop.mp4";
      fos = new FileOutputStream(new File(outputFilePath)); //throws Exception
      out.writeContainer(fos.getChannel());
      fos.close();
      mProgressDialog.dismiss();
      finish();

    } catch (Exception e) {
        Log.v("ONMESSAGE", e.toString());
      e.printStackTrace();
      mProgressDialog.dismiss();
      return false;
    }
    return true;
  }

You need to ask for write permission in your AndroidManifest.xml . 您需要在AndroidManifest.xml 请求写入权限 In particular, the following line must be present: 特别地,必须存在以下行:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

You shouldn't be looking at the Data Directory. 您不应该查看数据目录。 This is a system directory in the phone's storage - usually /data - and your application will never have permission to write to it. 这是电话存储器中的系统目录(通常为/ data),您的应用程序将永远无权对其进行写入。

The directory your application should write files to is returned by the Context.getFilesDir() method. 您的应用程序应将文件写入的目录由Context.getFilesDir()方法返回。 It will be something like /data/data/com.yourdomain.YourApp/files. 它将类似于/data/data/com.yourdomain.YourApp/files。

If you want to write to a file in the phone's storage use the Context.openFileOutput() method. 如果要写入手机存储中的文件,请使用Context.openFileOutput()方法。

If you want the path to the SDCard then use Environment.getExternalStorageDirectory() method. 如果您想要SD卡的路径,请使用Environment.getExternalStorageDirectory()方法。 To write to the SDCard you'll need to give your application the appropriate permissions by adding the following to your Manifest: 要写入SDCard,您需要通过向清单中添加以下内容来为您的应用程序授予适当的权限:

If you're going to write to the SDCard you'll also need to check its state with the getExternalStorageState() method. 如果要写入SDCard,还需要使用getExternalStorageState()方法检查其状态。

If you're storing small files to do with your application then these can go into the phone's storage and not the SD Card, so use the Context.openFileOutput() and Context.openFileInput() methods. 如果您要存储与应用程序有关的小文件,则这些文件可以进入手机的存储空间,而不是SD卡,因此请使用Context.openFileOutput()和Context.openFileInput()方法。

So in your code consider something like: 因此,在您的代码中考虑以下内容:

OutputStream os = openFileOutput("samplefile.txt", MODE_PRIVATE);
BufferedWriter lout = new BufferedWriter(new OutputStreamWriter(os));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RuntimeException:java.io.FileNotFoundException:(访问被拒绝)Android SDK - RuntimeException: java.io.FileNotFoundException: (Access is denied) Android SDK 无法更新 Android Studio - 拒绝访问和 java.io.FileNotFoundException - Can't update Android Studio - Access Denied and java.io.FileNotFoundException 生成失败-Android棉花糖SMS_SEND错误:等级:java.io.FileNotFoundException:(访问被拒绝) - Build fails - Android Marshmallow SMS_SEND Error:Gradle: java.io.FileNotFoundException: (Access is denied) 创建证书时我得到了 java.io.filenotfoundexception:myapplication.keystore<access is denied> 在 android</access> - when creating certificate i got java.io.filenotfoundexception:myapplication.keystore <access is denied> in android 错误:Android打包程序:[…] java.io.FileNotFoundException:…(权限被拒绝) - Error:Android Packager: […] java.io.FileNotFoundException:… (Permission denied) java.io.FileNotFoundException:(权限被拒绝)在Oreo Android中 - java.io.FileNotFoundException: (Permission denied) in oreo android Android内部缓存目录中的“java.io.FileNotFoundException:权限被拒绝” - “java.io.FileNotFoundException: Permission denied” in Android Internal Cache Dir Android - java.io.FileNotFoundException - Android - java.io.FileNotFoundException android中的java.io.FileNotFoundException - java.io.FileNotFoundException in android java.io.fileNotFoundException(权限被拒绝) - java.io.fileNotFoundException(Permission denied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM