0

I am trying to store my output file in internal memory.but it throws java.io.FileNotFoundException Access is denied

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;
  }
Sritam Jagadev
  • 955
  • 4
  • 18
  • 46

2 Answers2

1

You need to ask for write permission in your AndroidManifest.xml. In particular, the following line must be present:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Saran Tunyasuvunakool
  • 1,064
  • 1
  • 9
  • 23
  • Sir i want to store it in Internal Storage of my device.I have already added android.permission.WRITE_EXTERNAL_STORAGE permission in my manifest file. but its not working – Sritam Jagadev Mar 09 '15 at 11:57
  • Oops, my mistake. You can't write to `/data`. See http://stackoverflow.com/questions/1998400/data-directory-has-no-read-write-permission-in-android – Saran Tunyasuvunakool Mar 09 '15 at 11:59
  • No Sir. Some days before i have already checked my program.it worked fine.it was storing my output file to internal memory of my device.but now its showing exception in my logcat – Sritam Jagadev Mar 09 '15 at 12:03
  • Did you update your device since? (From 3.x to 4.x?) – RaphMclee Apr 17 '15 at 14:38
1

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.

The directory your application should write files to is returned by the Context.getFilesDir() method. It will be something like /data/data/com.yourdomain.YourApp/files.

If you want to write to a file in the phone's storage use the Context.openFileOutput() method.

If you want the path to the SDCard then use Environment.getExternalStorageDirectory() method. To write to the SDCard you'll need to give your application the appropriate permissions by adding the following to your Manifest:

If you're going to write to the SDCard you'll also need to check its state with the getExternalStorageState() method.

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.

So in your code consider something like:

OutputStream os = openFileOutput("samplefile.txt", MODE_PRIVATE);
BufferedWriter lout = new BufferedWriter(new OutputStreamWriter(os));
Akshay Paliwal
  • 3,718
  • 2
  • 39
  • 43