首页 > 解决方案 > How to upload audio file to internal storage?

问题描述

So I have a button called, "Attach audio effect." That is where I will get the location of my file and once I click "add", the mp3 file is uploaded to my /files/audio/

Here is an image of my file location

Yes, it successfully inserted but the file seems to be empty based on the size which says, "0 Bytes"

add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileInputStream fis;
            try {
                fis = new FileInputStream(new File(audioFxUri.getPath()));
                byte[] buf = new byte[1024];
                int n;
                while (-1 != (n = fis.read(buf)))
                    baos.write(buf, 0, n);
            } catch (Exception e) {
                e.printStackTrace();
            }
            byte[] bbytes = baos.toByteArray();

            //byte to file
            File output = new File(getFilesDir() + "/audio",audioFxUri.getPath());
            FileOutputStream fileoutputstream = null;
            try {
                fileoutputstream = new FileOutputStream(output);
                fileoutputstream.write(bbytes);
                fileoutputstream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

I've been stuck with this problem for almost a week now. Please help me! Thank you so much!

标签: android

解决方案


推荐阅读