2012-03-13 2 views
0

Я отчаянно пытаюсь скопировать файл на SD-карту из моей сырой папки, но это не сработает! Файл просто не отображается в диспетчере файлов, и программа, которую я даю пути (через намерение), также не может найти ее. Это то, что я пытаюсь ...Копирование файла на 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 –

+0

Проверьте разрешения на комментарий @SadeshkumarPeriyasamy «s. Кроме того, попробуйте создать выходной путь, используя 'FileOutputStream out = new FileOutputStream (новый файл (Environment.getExternalStorageDirectory()," /jazz.pdf "))' –

+0

@SadeshkumarPeriyasamy Да, у меня есть! В моем манифесте Android: user1123530

ответ

2

Просто напишите 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

Смежные вопросы