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();