gpt4 book ai didi

java - 将图像 Uri 保存为图像

转载 作者:行者123 更新时间:2023-12-01 14:20:46 25 4
gpt4 key购买 nike

所以我正在做的是从另一个应用程序接收数据作为 Intent 。我正在获取图像而不是尝试保存它

  void savefile(Uri sourceuri)
{
String sourceFilename= sourceuri.getPath();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "PhotoSaver");


BufferedInputStream bis = null;
BufferedOutputStream bos = null;

try {
bis = new BufferedInputStream(new FileInputStream(sourceFilename));
bos = new BufferedOutputStream(new FileOutputStream(mediaStorageDir, false));
byte[] buf = new byte[1024];
bis.read(buf);
do {
bos.write(buf);
} while(bis.read(buf) != -1);
} catch (IOException e) {

} finally {
try {
if (bis != null) bis.close();
if (bos != null) bos.close();
} catch (IOException e) {

}
}

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "PhotoSaver"))));

}

最佳答案

您应该一直阅读,直到到达输入流的末尾,并最终刷新输出流。像这样的事情:

     // the file is read to the end
while ((value = bis.read()) != -1) {
bos.write(value);
}

// invokes flush to force bytes to be written out to baos
bos.flush();

关于java - 将图像 Uri 保存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17584477/

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