2017-02-21 2 views
-2

なぜ機能しないのですか? 編集:コードの追加された新バージョン&ログ:Uri経由でJPGファイルをコピーする方法

private void savePhotoFromCacheToFolder(Uri uri) { 

    File goodPhoto = album.setUpPhotoFile(); //new empty JPG 
    File currentPhoto = new File(uri.getPath()); //JPG from camera in cache 

    Log.v(TAG, "\ngoodPhoto Path " + goodPhoto); 
    Log.v(TAG, "\ncurrentPhoto Path " + currentPhoto); 

    FileInputStream source = null; 
    FileOutputStream destination = null; 

    try { 
     source = new FileInputStream(currentPhoto); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Log.v(TAG, "\ncurrentPhoto not found "); 
    } 

    try { 
     destination = new FileOutputStream(goodPhoto); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Log.v(TAG, "\ngoodPhoto not found "); 
    } 

    FileChannel sourceFileChannel = source.getChannel(); 
    FileChannel destinationFileChannel = destination.getChannel(); 

    long size = 0; 
    try { 
     size = sourceFileChannel.size(); 
     sourceFileChannel.transferTo(0, size, destinationFileChannel); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.v(TAG, "\nshit happens "); 
    } 

} 

ログ:それがウリのように見える

V/MainActivity: goodPhoto Path /storage/emulated/0/Pictures/Good photos/IMG_20170222_113700_-913025224.jpg 
V/MainActivity: currentPhoto Path /cache/photo.jpg 
V/MainActivity: currentPhoto not found 
java.lang.NullPointerException: Attempt to invoke virtual method 'java.nio.channels.FileChannel java.io.FileInputStream.getChannel()' on a null object reference 

が正しくありませんが、このウリは、カメラアプリで返されました。または、私はキャッシュフォルダにアクセスできませんが、以前はこのUriを使用してプレビュー写真を作成しました。

+1

キャッチをいくつか追加すると、何が起こったのが表示されますか? – Selvin

+0

例外をキャッチして無視してもそれは非常に悪いです。 –

+0

@Selvin私たちが知る必要があるものは? – Nikita

答えて

0

入力ストリームと出力ストリームオブジェクトを作成しました。入力ストリームは現在のJavaファイルを指し、出力ストリームはOutput.javaを指しています。このOutput.javaに対して、ファイルの内容を転送する必要があります。前述のように、ファイルオブジェクトはファイルチャネルオブジェクトに関連付けられています。だから、私たちは

public copyFile(String filePath){ 


     FileInputStream source = new FileInputStream(filePath); 
     FileOutputStream destination = new FileOutputStream("Output.java"); 

     FileChannel sourceFileChannel = source.getChannel(); 
     FileChannel destinationFileChannel = destination.getChannel(); 

     long size = sourceFileChannel.size(); 
     sourceFileChannel.transferTo(0, size, destinationFileChannel); 
    } 

は、上記のコードを使用してコードを比較、次のコードを使用して、入力および出力ストリームの両方のファイルChannelオブジェクトを取得し、データを転送している間は、ここで使用されていないための方法の違いがあります。

関連する問題