android - 从 Assets 文件夹复制到 SD 卡

标签 android android-sdcard android-assets

我不确定这段代码有什么问题?它在 SD 卡中创建一个文件,但 0 字节 .. 你们中的一个人可以调查一下并告诉我这有什么问题吗?

我在这里尝试从 asset 文件夹复制一个文件,即 codes.db 到 sd 卡..

    AssetManager assetManager = getResources().getAssets();
    InputStream in = null;

    OutputStream out = null;
    BufferedInputStream buf = null;
    try {
        in = assetManager.open("codes.db");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("Error jh: " + e.getMessage());
    }
    if (in != null) {
        try {
            out = new FileOutputStream("/sdcard/" + "codes.db");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("Error Line : " + e.getLocalizedMessage());
        }
    }

    System.out.println("IN VALUE : " + in);
    System.out.println("OUT VALUE : " + out);


    int chunkBytes = 0;
    byte[] outputByte = new byte[1024];

    if (in != null && out != null) {
        try {

            // while ((chunkBytes = in.read(outputByte, 0, 1024)) != -1) {
            while ((chunkBytes = in.read()) != -1) {
                // write contents to the file
                System.out.println("I m here");
                // out.write(outputByte, 0, (int) chunkBytes);
                out.write(chunkBytes);
                // publish the progress of downloading..
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("Error here: "
                    + e.getLocalizedMessage().toString());
        }
    }

    try {
        in.close();
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("Error there: " + e.getLocalizedMessage());
    }

}

在所有情况下我都会收到此错误..我确定我已设置权限...SD 卡中的 codes.db 是 0 字节...:(

05-24 09:59:34.221: W/System.err(5559): java.io.IOException
05-24 09:59:34.221: W/System.err(5559):     at android.content.res.AssetManager.readAsset(Native Method)
05-24 09:59:34.221: W/System.err(5559):     at android.content.res.AssetManager.access$700(AssetManager.java:36)
05-24 09:59:34.221: W/System.err(5559):     at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
05-24 09:59:34.221: W/System.err(5559):     at com.bewo.copy.TestCopyActivity.copyStream(TestCopyActivity.java:136)
05-24 09:59:34.221: W/System.err(5559):     at com.bewo.copy.TestCopyActivity.onCreate(TestCopyActivity.java:19)
05-24 09:59:34.221: W/System.err(5559):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-24 09:59:34.221: W/System.err(5559):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2635)
05-24 09:59:34.221: W/System.err(5559):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2687)
05-24 09:59:34.221: W/System.err(5559):     at android.app.ActivityThread.access$2300(ActivityThread.java:127)
05-24 09:59:34.221: W/System.err(5559):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2035)
05-24 09:59:34.221: W/System.err(5559):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 09:59:34.221: W/System.err(5559):     at android.os.Looper.loop(Looper.java:123)
05-24 09:59:34.221: W/System.err(5559):     at android.app.ActivityThread.main(ActivityThread.java:4635)
05-24 09:59:34.221: W/System.err(5559):     at java.lang.reflect.Method.invokeNative(Native Method)
05-24 09:59:34.221: W/System.err(5559):     at java.lang.reflect.Method.invoke(Method.java:521)
05-24 09:59:34.221: W/System.err(5559):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-24 09:59:34.221: W/System.err(5559):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-24 09:59:34.221: W/System.err(5559):     at dalvik.system.NativeStart.main(Native

最佳答案

我希望下面的代码可以帮助你 -

package com.paresh.copyfileassetstoAssets;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class CopyFileAssetsToSDCardActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       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);   // if files resides inside the "Files" directory itself
              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);
        }
    }
}

最佳Blog用于将 Assets 文件复制到 SD 卡。并且,还要确保 External Storage 所需的权限

关于android - 从 Assets 文件夹复制到 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10730923/

相关文章:

android - WebView、启动画面和性能

android - 放大/缩小一些未渲染的图 block - Osmdroid 离线 map

android - 如何显示进度条几秒钟然后移动到 android 中的 Activity ?

android - 无法在我的代码中激活我的自定义权限

android - MP3 文件没有被推送到 Fedora Linux 中的 Android SD 卡中

android - 在android中扫描不同的内存类型

android - 如何检查sd卡目录下是否存在文件

android - 在多个项目之间共享 Assets

android - 如何避免对 APK Assets 文件夹资源项目文件进行逆向工程?

android - 按变量名启动 fragment