2012-03-13 21 views
0

未処理のフォルダからsdcardにファイルをコピーしようとしていますが、動作しません。ファイルはファイルマネージャーに表示されません。また、私が(インテント経由で)パスを指定したプログラムもそれを見つけることはできません。これは私が、私がしようとした後ファイルをsdcardにコピーする

private void CopyAssets() throws IOException { 
     String path = Environment.getExternalStorageDirectory() + "/jazz.pdf"; 
     InputStream in = getResources().openRawResource(R.raw.jazz); 
     FileOutputStream out = new FileOutputStream(path); 
     byte[] buff = new byte[1024]; 
     int read = 0; 
    try { 
     while ((read = in.read(buff)) > 0) { 
      out.write(buff, 0, read); 
     } 
    } finally { 
     in.close(); 

     out.close(); 
    } 
} 

...

try { 
     CopyAssets(); 

    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 

    String aux = Environment.getExternalStorageDirectory() + "/jazz.pdf"; 

    Uri path = Uri.parse(aux); 

    Intent intent = new Intent(Intent.ACTION_VIEW); 

    intent.setDataAndType(path, "application/pdf"); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    try { 

     startActivity(intent); 

    } catch (ActivityNotFoundException e) { 

     Toast.makeText(bgn1.this, "No Application Available to View PDF", 
       Toast.LENGTH_SHORT).show(); 
    } 
+1

あなたは用途許可android.permission.WRITE_EXTERNAL_STORAGEに@SadeshkumarPeriyasamyのコメントあたり –

+0

チェック権限を与えています。また、 'FileOutputStream out = new FileOutputStream(new File(Environment.getExternalStorageDirectory()、" /jazz.pdf "))' –

+0

@SadeshkumarPeriyasamyを使用して出力パスを作成してみてください。私のアンドロイドマニフェストで: user1123530

答えて

2

わずか1ブロック、私が起こるか知っている聞かせて、最終的でout.flush();を書く...しようとしているものです

finally { 
     out.flush(); 
     in.close(); 
     out.close(); 
     } 

更新:

作業コード:

private void CopyAssets() throws IOException 
{  
    InputStream myInput = getResources().openRawResource(R.raw.jazz); 
    String outFileName = Environment.getExternalStorageDirectory() + "/jazz.pdf"; 
    OutputStream myOutput = new FileOutputStream(outFileName); 
    // transfer bytes from the input file to the output file 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer)) > 0) 
    { 
     myOutput.write(buffer, 0, length); 
    } 
    // Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 
    } 
} 
+0

ありがとうございました! :)それはそれを解決しました。 – user1123530

関連する問題