美文网首页
拷贝assets内容到安卓手机

拷贝assets内容到安卓手机

作者: 汶水一方 | 来源:发表于2017-08-18 07:53 被阅读38次
    1. 新建一个Android Studio项目。
    2. 在main文件夹下,新建assets文件夹,得到app/src/main/assets。把需要拷贝的文件复制/粘贴到其中。
      我这里放了2个文件,01.jpg和02.jpg。
    1. 在AndroidManifest.xml中,授予 写外部存储 的权限。
      在<application>外加入:
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    1. 修改MainActivity.java文件。
    package ai.nixie.copyassets;
    
    /*
    
    Copy all the files in assets folder to your smartphone
    
    From: app/src/main/assets
    To:   /sdcard/Android/data/packageName/files/
    
    */
    
    import android.content.res.AssetManager;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            copyAssets();
    
        }
    
        private void copyAssets() {
            AssetManager assetManager = getAssets();
            String[] files = null;
            try {
                files = assetManager.list("");
            } catch (IOException e) {
                Log.e("tag", "Failed to get asset file list.", e);
            }
            if (files != null) for (String filename : files) {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = assetManager.open(filename);
                    File outFile = new File(getExternalFilesDir(null), filename);
                    out = new FileOutputStream(outFile);
                    copyFile(in, out);
                } catch(IOException e) {
                    Log.e("tag", "Failed to copy asset file: " + filename, e);
                }
                finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            // NOOP
                        }
                    }
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e) {
                            // NOOP
                        }
                    }
                }
            }
        }
        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);
            }
        }
    }
    
    1. 验证。
      开命令行,adb shell进入手机,可以在/sdcard/Android/data/包名/files/目录中,找到我们要拷贝的文件。
    1. 几个常用的目录
    String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
    Log.d(TAG, "getExternalStorageDirectory: " + sdcardPath);
    // getExternalStorageDirectory: /storage/emulated/0
    
    String dataDir = getFilesDir().toString();
    Log.d(TAG, "getFilesDir(): " + dataDir);
    //getFilesDir(): /data/user/0/ai.nixie.copyassets/files
    
    String externalFilesDir = getExternalFilesDir(null).toString();
    Log.d(TAG, "getExternalFilesDir: " + externalFilesDir);
    //getExternalFilesDir: /storage/emulated/0/Android/data/ai.nixie.copyassets/files
    
    
    PackageManager packageManager = getPackageManager();
    String str =getPackageName();
    try {
        PackageInfo pi = packageManager.getPackageInfo(str, 0);
        str = pi.applicationInfo.dataDir;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    Log.d(TAG, "applicationInfo.dataDir: " + str);
    //applicationInfo.dataDir: /data/user/0/ai.nixie.copyassets
    
    

    Android Studio v2.3中测试通过。

    References
    https://stackoverflow.com/questions/4447477/how-to-copy-files-from-assets-folder-to-sdcard

    相关文章

      网友评论

          本文标题:拷贝assets内容到安卓手机

          本文链接:https://www.haomeiwen.com/subject/tmyorxtx.html