gpt4 book ai didi

java - 复制 Android 文件时找不到文件异常

转载 作者:行者123 更新时间:2023-12-01 15:33:17 25 4
gpt4 key购买 nike

我正在尝试使用此代码。

public synchronized void onActivityResult(final int requestCode, int resultCode,
final Intent data) {
String filePath = null;
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case RESULT_MEMORY_SAVE:
filePath = data.getDataString();
copyFileForReceipts(filePath,pathOfReceipts);

现在 filePath 是一个 String ,调试器将其值显示为 "content://media/external/images/media/133"

接下来我尝试使用这个:

sourceLocation  = filePath;
File afile = new File(sourceLocation);
File bfile = new File(targetLocation + "file.jpg");
inStream = new FileInputStream(afile.getName()); //Gives exception
outStream = new FileOutputStream(bfile.getName());

它给出了 FileNotFound 异常。我也尝试删除 .getName()

请告诉我哪里错了。

问候

最佳答案

您应该输入文件路径并按如下方式检索它,而不是使用setDatagetDataString:

在结果生成 Activity 中添加路径:

intent.putString("path", file_path);

并检索路径:

filePath = data.getString("path");
copyFileForReceipts(filePath,pathOfReceipts);

在对其进行进一步操作之前检查文件是否存在是一个很好的做法:

if(afile.exists()){
//do whatever you want
}

关于java - 复制 Android 文件时找不到文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9289637/

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