【android】androidk获取assets内容复制粘贴本地存储

新建的项目,一定要记得添加读写文件的权限啊,不然没有效果的,你不加权限,没有效果请不要找我

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

(1)我们首先需要创建assets文件夹,如图所示,在project状态下,鼠标选中main文件夹,右键单击,然后依次选择我们的内容,最后点击确定,assets就创建好了

【android】androidk获取assets内容复制粘贴本地存储

 

然后我们在assets文件夹下面新建一个文件夹叫folder,把我们需要复制的文件放到此文件夹下面,我们放了三个不同的文件,如图所示:

【android】androidk获取assets内容复制粘贴本地存储

 

(2)复制assets文件到内部存储中

package com.android.example.assets;

import android.content.Context;

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 Utils{

public static void doCopy(Context context, String assetsPath, String desPath) throws IOException {

String[] srcFiles = context.getAssets().list(assetsPath);//for directory

for (String srcFileName : srcFiles) {

String outFileName = desPath + File.separator + srcFileName;

String inFileName = assetsPath + File.separator + srcFileName;

if (assetsPath.equals("")) {// for first time

inFileName = srcFileName;

}

Log.e("tag","========= assets: "+ assetsPath+" filename: "+srcFileName +" infile: "+inFileName+" outFile: "+outFileName);

try {

InputStream inputStream = context.getAssets().open(inFileName);

copyAndClose(inputStream, new FileOutputStream(outFileName));

} catch (IOException e) {//if directory fails exception

e.printStackTrace();

new File(outFileName).mkdir();

doCopy(context,inFileName, outFileName);

}

}

}

private static void closeQuietly(OutputStream out){

try{

if(out != null) out.close();;

}catch (IOException ex){

ex.printStackTrace();

}

}

private static void closeQuietly(InputStream is){

try{

if(is != null){

is.close();

}

}catch (IOException ex){

ex.printStackTrace();

}

}

private static void copyAndClose(InputStream is, OutputStream out) throws IOException{

copy(is,out);

closeQuietly(is);

closeQuietly(out);

}

private static void copy(InputStream is, OutputStream out) throws IOException{

byte[] buffer = new byte[1024];

int n = 0;

while(-1 != (n = is.read(buffer))){

out.write(buffer,0,n);

}

}

}

 

这个时候,请你们打开手机的内部存储,文件在Android/data/项目包名/files/  里面就是啦,反正我的手机是有的,你们试一下,按照我的步骤来,肯定也是有的,到这里我们的复制就成功了

 

(3)计算文件大小的类

package com.example.administrator.testz;

import android.util.Log;

import java.io.File;

import java.io.FileInputStream;

import java.text.DecimalFormat;

/**

* @CreateBy HaiyuKing

* @Used android 获取文件夹或文件的大小 以B、KB、MB、GB 为单位

* @参考资料 http://blog.csdn.net/jiaruihua_blog/article/details/13622939

*/

public class FileSizeUtil {

public static final int SIZETYPE_B = 1;//获取文件大小单位为B的double值

public static final int SIZETYPE_KB = 2;//获取文件大小单位为KB的double值

public static final int SIZETYPE_MB = 3;//获取文件大小单位为MB的double值

public static final int SIZETYPE_GB = 4;//获取文件大小单位为GB的double值

/**

* 获取指定文件或指定文件夹的的指定单位的大小

* @param filePath 文件路径

* @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB

* @return double值的大小

*/

public static double getFolderOrFileSize(String filePath,int sizeType){

File file=new File(filePath);

long blockSize=0;

try {

if(file.isDirectory()){

blockSize = getFolderSize(file);

}else{

blockSize = getFileSize(file);

}

} catch (Exception e) {

e.printStackTrace();

Log.e("获取文件大小","获取失败!");

}

return FormetFileSize(blockSize, sizeType);

}

public static double getsizeFolderOrFileSize(File file,int sizeType){

long blockSize=0;

try {

if(file.isDirectory()){

blockSize = getFolderSize(file);

}else{

blockSize = getFileSize(file);

}

} catch (Exception e) {

e.printStackTrace();

Log.e("获取文件大小","获取失败!");

}

return FormetFileSize(blockSize, sizeType);

}

/**

* 调用此方法自动计算指定文件或指定文件夹的大小

* @param filePath 文件路径

* @return 计算好的带B、KB、MB、GB的字符串

*/

public static String getAutoFolderOrFileSize(String filePath){

File file=new File(filePath);

long blockSize=0;

try {

if(file.isDirectory()){

blockSize = getFolderSize(file);

}else{

blockSize = getFileSize(file);

}

} catch (Exception e) {

e.printStackTrace();

Log.e("获取文件大小","获取失败!");

}

return FormetFileSize(blockSize);

}

/**

* 获取指定文件的大小

* @param file

* @return

* @throws Exception

*/

private static long getFileSize(File file) throws Exception

{

long size = 0;

if (file.exists()){

FileInputStream fis = null;

fis = new FileInputStream(file);

size = fis.available();

fis.close();

}

else{

file.createNewFile();

Log.e("获取文件大小","文件不存在!");

}

return size;

}

/**

* 获取指定文件夹的大小

* @param file

* @return

* @throws Exception

*/

private static long getFolderSize(File file) throws Exception

{

long size = 0;

File flist[] = file.listFiles();

for (int i = 0; i < flist.length; i++){

if (flist[i].isDirectory()){

size = size + getFolderSize(flist[i]);

}

else{

size =size + getFileSize(flist[i]);

}

}

return size;

}

/**

* 转换文件大小

* @param fileSize

* @return

*/

private static String FormetFileSize(long fileSize)

{

DecimalFormat df = new DecimalFormat("#.00");

String fileSizeString = "";

String wrongSize="0B";

if(fileSize==0){

return wrongSize;

}

if (fileSize < 1024){

fileSizeString = df.format((double) fileSize) + "B";

}

else if (fileSize < 1048576){

fileSizeString = df.format((double) fileSize / 1024) + "KB";

}

else if (fileSize < 1073741824){

fileSizeString = df.format((double) fileSize / 1048576) + "MB";

}

else{

fileSizeString = df.format((double) fileSize / 1073741824) + "GB";

}

return fileSizeString;

}

/**

* 转换文件大小,指定转换的类型

* @param fileSize

* @param sizeType

* @return

*/

private static double FormetFileSize(long fileSize,int sizeType)

{

DecimalFormat df = new DecimalFormat("#.00");

double fileSizeLong = 0;

switch (sizeType) {

case SIZETYPE_B:

fileSizeLong=Double.valueOf(df.format((double) fileSize));

break;

case SIZETYPE_KB:

fileSizeLong=Double.valueOf(df.format((double) fileSize / 1024));

break;

case SIZETYPE_MB:

fileSizeLong=Double.valueOf(df.format((double) fileSize / 1048576));

break;

case SIZETYPE_GB:

fileSizeLong=Double.valueOf(df.format((double) fileSize / 1073741824));

break;

default:

break;

}

return fileSizeLong;

}

}

(4)在主文件中调用,那个路径写死了 是我的手机的路径  你们可能需要改一下才可以使用,写完以后运行吧,有一个重要的log日志,是关于打印文件路径的,这个路径就是复制到手机内存的路径啦

package com.example.administrator.testz;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import java.io.File;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private Button btn_txt,button;

private EditText et;

private TextView txt;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn_txt = (Button)findViewById(R.id.btn_txt);

button= (Button)findViewById(R.id.button);

et = (EditText)findViewById(R.id.et);

txt = (TextView) findViewById(R.id.txt);

/*

/storage/emulated/0/Android/data/com.example.administrator.testz/files/aspcone.jsah

/storage/emulated/0/Android/data/com.example.administrator.testz/files/like.png

/storage/emulated/0/Android/data/com.example.administrator.testz/files/test.txt

*/

et.setText("/storage/emulated/0/Android/data/com.example.administrator.testz/files/aspcone.jsah");

btn_txt.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

doCopy();

} catch (IOException e) {

e.printStackTrace();

}

}

});

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

String pathname = et.getText().toString();

double fileSizeKB = FileSizeUtil.getFolderOrFileSize(pathname,FileSizeUtil.SIZETYPE_KB);

String fileSize = FileSizeUtil.getAutoFolderOrFileSize(pathname);

txt.setText("fileSizeKB=" + fileSizeKB + "\n" +

"fileSize=" + fileSize);

}

});

}

private void doCopy() throws IOException{

File externalDir = this.getExternalFilesDir(null);

Utils.doCopy(this,"folder",externalDir.getPath());

}

}

最后贴上我们的运行效果:

【android】androidk获取assets内容复制粘贴本地存储

大致就是这么个效果啦,如果需要源码的同学,请关注我,进入我的csdn资源,我已经把源码上传到csdn了

以上是 【android】androidk获取assets内容复制粘贴本地存储 的全部内容, 来源链接: utcz.com/a/68828.html

回到顶部