마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 파일복사질문!

0 추천
public void copyFile(String mainPath,String extPath){
String abPath=Environment.getExternalStorageDirectory().getPath();
File mainFile=new File(mainPath);
File extFile=new File(extPath);
String fileName=new File(extPath).getName();

FileInputStream inputStream=null;
try{
 inputStream=new FileInputStream(mainFile);
}catch(FileNotFoundException e){
 e.printStackTrace();
}
FileOutputStream outputStream=null;
try{
 outputStream=new FileOutputStream(extFile);
 }catch(FileNotFoundException e){
  e.printStackTrace();
 }
 FileChannel fcin=inputStream.getChannel();
 FileChannel fcout=outputStream.getChannel();
 long size = 0;
                try {
                    size = fcin.size();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    fcin.transferTo(0, size, fcout);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    fcout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    fcin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
}

 

이 코드로 제폰에서 파일을 복사하고있는데요 기본저장소에 복사하는건 되는데 외부저장소(sd카드)에 복사하면 에러가뜰까요...?

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:29828 flg=0x43 }} to activity {com.example.joseph.gellery_image/com.example.joseph.gellery_image.EditActivity_1}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.nio.channels.FileChannel java.io.FileOutputStream.getChannel()' on a null object reference

위처럼 에러가뜨네요...
JesusLovesYou (2,700 포인트) 님이 2016년 1월 12일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...