2012-05-24 5 views
1

이 코드에 어떤 문제가 있는지 잘 모르겠습니다. 그것은 SD 카드에 있지만 0 바이트로 파일을 만듭니다. 당신 중 하나가 그걸 살펴보고 이것에 무슨 문제가 있다고 말해 줄 수 있습니까? 내가 SD 카드에 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 
+0

안녕하세요 라훌, 내가 무엇입니까 로그 내 질문을 편집 한 http://stackoverflow.com/questions/9109438/using-already-created-database-with-android/9109728#9109728 –

답변

1

사용 어떤 AVD 관리자에서 SD 카드의 크기를 증가보십시오. 를 내가 당신을 도와 바랍니다.

+0

... –

2

내가 코드를 아래에 희망 수도가 당신을 도와줍니다 - sdcard에 복사 할 자산 파일

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. 그리고, 또한

+0

인사 SpK, 내가 받고있는 로그로 내 질문을 편집했습니다 ... –

+0

@SanjayHerle 어떤 유형의 파일을 복사하려고합니까? 그리고 내 대답을 시험해 보셨습니까? – Praveenkumar

+0

네, 다른 날에 블로그를 보았습니다. 나는 그걸 시도했지만, 불행히도 나를 위해 성공하지 못했습니다. 뭔가를 놓칠 수 있습니다 .. –

2

많은 검색 후, 나는 단순히 내가 나중에 스레드에서 사용할 수있는 방법을 원 External Storage 위해 반드시 필요한 권한을 만들어, 여기에 내가 내 응용 프로그램을 위해 사용하고 무엇을 :

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) { 
      } 
     } 
    } 
} 

하세요 이게 도움이되는지 알려주세요!

+0

좋은 방법, 시간 내 주셔서 감사합니다. –

관련 문제