Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过Uri复制JPG文件_Android_File - Fatal编程技术网

Android 如何通过Uri复制JPG文件

Android 如何通过Uri复制JPG文件,android,file,Android,File,为什么它不起作用? 编辑:添加了新版本的代码和日志: 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 P

为什么它不起作用? 编辑:添加了新版本的代码和日志:

    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不正确,但此Uri是由Camera应用程序返回的。或者我没有访问缓存文件夹的权限,但之前我确实使用此Uri预览了照片。

我们创建了一个输入流和一个输出流对象。输入流指向当前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);
    }

将您的代码与上述代码进行比较,传输数据的方法不同,此处不使用while。

在catch中添加一些日志记录,您将看到发生了什么,跟踪异常并忽略它是非常糟糕的。@Selvin我们需要知道什么?在
catch
中添加日志并记录异常(如果有)。我不知道这有多合法,但这是works
source=(FileInputStream)UriHelper.openInputStream(这是uri)