android - java.io.FileNotFoundException : output. mp4(只读文件系统)

标签 android animation

我正在尝试将我的图像转换成视频。我找到了示例代码并将其用于我的应用程序。但是我收到了这样的错误

java.io.FileNotFoundException: output.mp4(只读文件系统)

这是我的代码 fragment :

    public class Main2Activity extends AppCompatActivity {

        AndroidSequenceEncoder encoder;
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);

            ArrayList<Bitmap> arrayList = new ArrayList<>();
            Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.empty_collage);
            Bitmap a = BitmapFactory.decodeResource(getResources(),R.drawable.images);

            arrayList.add(a);
            arrayList.add(b);

            File file = new File("output.mp4");

            SeekableByteChannel out = null;
            try {
                out = (SeekableByteChannel) NIOUtils.writableFileChannel(String.valueOf(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Log.e("msg",e.toString());
            }

            try {
                 encoder = new AndroidSequenceEncoder((org.jcodec.common.io.SeekableByteChannel) out, Rational.R(25,1));
            } catch (IOException e) {
                e.printStackTrace();
            }

            Toast.makeText(this, "in for loop", Toast.LENGTH_SHORT).show();
            for(int i=0 ;i<arrayList.size() ; i++){
                try {
                    encoder.encodeImage(arrayList.get(i));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Toast.makeText(this, "out of loop", Toast.LENGTH_SHORT).show();
            try {
                encoder.finish();
            } catch (IOException e) {
                e.printStackTrace();
            }
            NIOUtils.closeQuietly(out);
        }
     }
   }

希望我能很快得到回复。 谢谢。

最佳答案

File file = new File("output.mp4");

这并没有指向任何有用的地方。始终使用一种方法来导出要写入的根位置。

例如,您可以将其更改为:

File file = new File(getFilesDir(), "output.mp4");

或:

File file = new File(getExternalFilesDir(null), "output.mp4");

关于android - java.io.FileNotFoundException : output. mp4(只读文件系统),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46364126/

相关文章:

android - 如何解决 java.lang.IndexOutOfBoundsException : Invalid index 0, 大小为 0?

java - 在 Android 上拍照时发生崩溃。为什么?

css - onHover 的反转动画

java - Realm :迭代 RealmObject 并清除 ArrayList 字段

android - 在android中删除sharedPreferences

android - 如何在Android中加密和解密文件?

javascript - Canvas 的 drawImage() 不进行子像素渲染/抗锯齿是否存在技术原因?

ios - 动画按钮的最佳方式是什么?

javascript - jQuery Animate 在中间快速旋转和滚动文本但在结束时慢

ios - 使用 CATransformLayer 警告 : changing property opaque in transform-only layer, 将无效