从资产文件夹复制到SD卡复制 [英] Copying from asset folder to SD card
本文介绍了从资产文件夹复制到SD卡复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道什么是错的这个code?它创建于SD卡,但以0字节的文件..
你可以查看一下,告诉我什么问题呢?
下面我试图复制从资源文件夹的文件位于codes.db到SD卡。
AssetManager assetManager = getResources()getAssets()。
在的InputStream = NULL; 出的OutputStream = NULL;
BUF的BufferedInputStream = NULL;
尝试{
在= assetManager.open(codes.db); }赶上(IOException异常五){
// TODO自动生成catch块
的System.out.println(错误JH:+ e.getMessage());
}
如果(在!= NULL){
尝试{
OUT =新的FileOutputStream(/ SD卡/+codes.db);
}赶上(FileNotFoundException异常五){
// TODO自动生成catch块
的System.out.println(错误行:+ e.getLocalizedMessage());
}
} 的System.out.println(IN值:+的);
的System.out.println(OUT值:+ OUT);
INT chunkBytes = 0;
字节[] = outputByte新的字节[1024]; 如果(在= NULL和放大器;!&安培;!OUT = NULL){
尝试{ //而((chunkBytes = in.read(outputByte,0,1024))!= -1){
而((chunkBytes = in.read())!= - 1){
//写入内容文件
的System.out.println(我为在这里);
// out.write(outputByte,0,(int)的chunkBytes);
out.write(chunkBytes);
//发布下载进度..
}
}赶上(例外五){
// TODO自动生成catch块
的System.out.println(错误的位置:
+ e.getLocalizedMessage()的toString());
}
} 尝试{
附寄();
out.close();
}赶上(IOException异常五){
// TODO自动生成catch块
的System.out.println(错误有:+ e.getLocalizedMessage());
}}
我收到此错误在所有的情况下,我确定我有我的权限集... SD卡codes.db为0字节...:(
九月五日至24日:59:34.221:W / System.err的(5559):java.io.IOException异常
9月5日至24日:59:34.221:W / System.err的(5559):在android.content.res.AssetManager.readAsset(本机方法)
9月5日至24日:59:34.221:W / System.err的(5559):在android.content.res.AssetManager.access $ 700(AssetManager.java:36)
9月5日至24日:59:34.221:W / System.err的(5559):在android.content.res.AssetManager $ AssetInputStream.read(AssetManager.java:571)
9月5日至24日:59:34.221:W / System.err的(5559):在com.bewo.copy.TestCopyActivity.copyStream(TestCopyActivity.java:136)
9月5日至24日:59:34.221:W / System.err的(5559):在com.bewo.copy.TestCopyActivity.onCreate(TestCopyActivity.java:19)
9月5日至24日:59:34.221:W / System.err的(5559):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
9月5日至24日:59:34.221:W / System.err的(5559):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2635)
9月5日至24日:59:34.221:W / System.err的(5559):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2687)
9月5日至24日:59:34.221:W / System.err的(5559):在android.app.ActivityThread.access $ 2300(ActivityThread.java:127)
9月5日至24日:59:34.221:W / System.err的(5559):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2035)
9月5日至24日:59:34.221:W / System.err的(5559):在android.os.Handler.dispatchMessage(Handler.java:99)
9月5日至24日:59:34.221:W / System.err的(5559):在android.os.Looper.loop(Looper.java:123)
9月5日至24日:59:34.221:W / System.err的(5559):在android.app.ActivityThread.main(ActivityThread.java:4635)
9月5日至24日:59:34.221:W / System.err的(5559):在java.lang.reflect.Method.invokeNative(本机方法)
9月5日至24日:59:34.221:W / System.err的(5559):在java.lang.reflect.Method.invoke(Method.java:521)
9月5日至24日:59:34.221:W / System.err的(5559):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
9月5日至24日:59:34.221:W / System.err的(5559):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
9月5日至24日:59:34.221:W / System.err的(5559):在dalvik.system.NativeStart.main(母语
解决方案
许多搜索之后,我只是想我可以在一个线程以后使用的方法,这里是我在用我的应用程序:
的静态公共无效copyFileFromAssets(上下文的背景下,字符串文件,字符串DEST)抛出异常
{
在的InputStream = NULL;
OutputStream的FOUT = NULL;
诠释计数= 0; 尝试
{
在= context.getAssets()打开(文件)。
FOUT =新的FileOutputStream(新文件(DEST)); 字节的数据[] =新的字节[1024];
而((计数= in.read(数据,0,1024))!= -1)
{
fout.write(数据0,计);
}
}
赶上(例外五)
{
e.printStackTrace();
}
最后
{
如果(在!= NULL)
{
尝试{
附寄();
}赶上(IOException异常E)
{
}
}
如果(FOUT!= NULL)
{
尝试{
fout.close();
}赶上(IOException异常五){
}
}
}
}
请让我知道如果这有助于!
I am not sure what is wrong with this code? It creates a file in SD card but with 0 byte.. Can one of you look into it and tell me whats wrong with this?
Here i am trying to copy a file from asset folder which is codes.db to sd card..
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());
}
}
I am getting this error in all the case.. i am sure i have my permission set... codes.db in sd card is 0 byte... :(
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
解决方案
After many searches, I simply wanted a method I could use in a thread later on, here is what I am using for my application:
static public void copyFileFromAssets(Context context, String file, String dest) throws Exception
{
InputStream in = null;
OutputStream fout = null;
int count = 0;
try
{
in = context.getAssets().open(file);
fout = new FileOutputStream(new File(dest));
byte data[] = new byte[1024];
while ((count = in.read(data, 0, 1024)) != -1)
{
fout.write(data, 0, count);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (in != null)
{
try {
in.close();
} catch (IOException e)
{
}
}
if (fout != null)
{
try {
fout.close();
} catch (IOException e) {
}
}
}
}
Please let me know if this helps!
这篇关于从资产文件夹复制到SD卡复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文