gpt4 book ai didi

android - 从 Uri 创建文件

转载 作者:行者123 更新时间:2023-12-03 02:43:10 26 4
gpt4 key购买 nike

我正在尝试将图像上传到 php,为此我需要将文件发送到服务器。所以我尝试从数据参数创建一个文件。

但我收到此错误无法解析构造函数文件

这是我的代码:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
// Get the url from data
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// File
File imageFilePath = new File(selectedImageUri);

最佳答案

正确的方法是getContentResolver().openInputStream(uri_of_your_file);并使用 FileOutputStream 到您所需的路径,然后使用该文件。

Commonsware answer

You can use ContentResolver and openInputStream() to get an InputStream on the content represented by the Uri. You can create a FileOutputStream on some file that you control. And, you can use Java I/O to copy from the InputStream to the OutputStream, making your own copy of the content in a file that you control.

执行此操作的示例代码,

InputStream in =  getContentResolver().openInputStream("your_uri_here");
OutputStream out = new FileOutputStream(new File("your_file_here"));
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.close();
in.close();

关于android - 从 Uri 创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45520599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com