0

this is MyNewMain.java

        CopyAssets();


    private void CopyAssets() {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try {
            files = assetManager.list("Files");
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        for(String filename : files) {
            System.out.println("File name => "+filename);
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open("Files/"+filename);
                out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch(Exception e) {
                Log.e("tag", e.getMessage());
            }
        }
    }
    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

in my assets folder i have folder called Files. there is .txt file onCreate methed i am calling CopyAssets();

lower are the methods i used.

the problem is that this does nothing. i have absoluteli no idea why my file is not being copied. in my manifest i have added

and app.iml contains

user2864772
  • 53
  • 1
  • 4
  • 12
  • Nothing? `System.out.println("File name => "+filename);`. What does that print? Why are you posting so much code that has nothing to do with your problem? Only CopyAssets() would do. Please edit your post. – greenapps Nov 23 '14 at 23:49
  • `Environment.getExternalStorageDirectory().toString()`. Try `Environment.getExternalStorageDirectory().getAbsolutePath()`. – greenapps Nov 23 '14 at 23:55
  • i have tried .getAbsolutePath() that greenapps suggested but it didnt work – user2864772 Nov 24 '14 at 06:25
  • this code is perfect..check whether you have set the required permissions in manifest.xml – vinv Nov 24 '14 at 07:25
  • yes, i have setthat in my manifest – user2864772 Nov 24 '14 at 08:08
  • You still have not answered my first question. Why? Add more Log statements to your code and tell what all gets printed. Are there catches? It's time you post the LogCat too. Tell exactly how your code flows or gets executed step by step. – greenapps Nov 24 '14 at 08:44
  • `files = assetManager.list("Files")`. Do not use uppercase in assets file or folder names? Change in Eclipse to "files" and use `files = assetManager.list("files");`. – greenapps Nov 24 '14 at 08:50
  • i am using android studio. i have renamed the folder to files. still nothing good. why cant i use upper case letter? i have putted Log.v(LOG_TAG, "333333"); on the end of copyfile method, i cant find it in my logcat. so that means is not being called. i am posting my full logcat for greenapps, dunno why he would need it – user2864772 Nov 24 '14 at 11:23
  • `why cant i use upper case letter?`. I did not say as much. I only suggested you to find out if that was the problem. The posted logcat is useless. You should add a lot more Log statements to CopyAssets(). Certainly the first one at the beginning. And a Log before you call it and a Log after the call. And then post here the logcat for a call to CopyAssets(). Add e.PrintStackTrace to the catch block. Make it to return a boolean which you can inspect after calling it. return false in the catch block. You should just do some basic debugging in this way. – greenapps Nov 24 '14 at 13:36

2 Answers2

-1

I know this has been answered but I have a slightly more elegant way to copy from asset directory to a file on the sdcard. It requires no "for" loop but instead uses File Streams and Channels to do the work.

(Note) If using any type of compressed file, APK, PDF, ... you may want to rename the file extension before inserting into asset and then rename once you copy it to SDcard)

AssetManager am = context.getAssets();
AssetFileDescriptor afd = null;
try {
    afd = am.openFd( "MyFile.dat");

    // Create new file to copy into.
    File file = new File(Environment.getExternalStorageDirectory() + java.io.File.separator + "NewFile.dat");
    file.createNewFile();

    copyFdToFile(afd.getFileDescriptor(), file);

} catch (IOException e) {
    e.printStackTrace();
}

A way to copy a file without having to loop through it.

public static void copyFdToFile(FileDescriptor src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

Thanks to JPM's Answer

Community
  • 1
  • 1
Salmaan
  • 3,543
  • 8
  • 33
  • 59
-1

on android api 22, call getFileDescriptor() on a AssetFileDescriptor object will return the FileDescriptor of whole apk file. so @Salmaan's answer is wrong on android api 22.

I don not see the source code in other api. So i don't know the behaviour of getFileDescriptor() in other api.

I find the answer under this question.

Community
  • 1
  • 1
albuscrow
  • 61
  • 1
  • 6