Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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转换为文件?_Android - Fatal编程技术网

Android 如何将其他应用程序共享的图像Uri转换为文件?

Android 如何将其他应用程序共享的图像Uri转换为文件?,android,Android,我总是将文件设置为空。我知道这是不对的。我错过了什么 您应该从Uri获取InputStream,然后将其保存到文件中。类似这样的内容(代码可能需要一些改进): 你能试一下下面的代码吗 private void handleImageData(Uri data) { if(data != null){ file = new File(data.getPath()); capturedImageView.setImageURI(data); }

我总是将文件设置为空。我知道这是不对的。我错过了什么

您应该从
Uri
获取
InputStream
,然后将其保存到文件中。类似这样的内容(代码可能需要一些改进):


你能试一下下面的代码吗
 private void handleImageData(Uri data) {
    if(data != null){
        file = new File(data.getPath());
        capturedImageView.setImageURI(data);
    }
    else {
        AndyUtils.showErrorToast("Invalid Uri", this );
    }
}
File file = new File("path");

//get InputStream
ContentResolver cr = context.getContentResolver();
InputStream is = cr.openInputStream( fileUri );

//write to file
FileOutputStream fos = new FileOutputStream( file );
byte[] buffer = new byte[ 1024 ];
int count;
while ( ( count = is.read( buffer ) ) != -1 ) {
    fos.write( buffer, 0, count );
    fos.flush();
}

//close everything
fos.close();
is.close();