[英]Load file using C++ std::ifstream.getline() in Android
I'm struggling with reading file stream inside Android environment using C++ library.我正在努力使用 C++ 库在 Android 环境中读取文件 stream 。 I believe I set all the permissions correctly (1st figure) and I'm getting the file path using Android internal library.
我相信我正确设置了所有权限(第一个图),并且我正在使用 Android 内部库获取文件路径。 Would you please give me a snippet to correctly read a file using std::ifstream.getline()?
请给我一个片段以使用 std::ifstream.getline() 正确读取文件吗? For instance, I get "/document/1EF7-1509:Download/015_1440.jpg" for the image file existing in the 2nd figure under "Download" folder.
例如,我得到“/document/1EF7-1509:Download/015_1440.jpg”作为“下载”文件夹下第二个图中存在的图像文件。 This path is a raw value returned by "Intent.getData().getPath()" with "ACTION_GET_CONTENT".
该路径是由“Intent.getData().getPath()”和“ACTION_GET_CONTENT”返回的原始值。
extern "C" JNIEXPORT jstring JNICALL Java_com_example_testpplication_MainActivity_testGeneralEncryption(JNIEnv* env, jobject, jstring myString)
{
const char *nativeString = env->GetStringUTFChars(myString, nullptr);
std::string filePath = std::string(nativeString);
std::string buffer;
std::ifstream fStreamIn(filePath);
if(fStreamIn.is_open())
{
std::getline(fStreamIn, buffer);
}
else
{
bool exists = fStreamIn.good();
if(exists)
{
buffer = "Exists";
}
else
{
buffer = "Non-existing";
}
}
return env->NewStringUTF((buffer + ":" + filePath).c_str());
}
Thanks to @blackapps, I did a google with a different keyword and found below answer.感谢@blackapps,我使用不同的关键字进行了谷歌搜索,并找到了以下答案。 Basically, my question is a duplicate.
基本上,我的问题是重复的。
Inspired by this answer: https://stackoverflow.com/a/49221353/1770003受此答案的启发: https://stackoverflow.com/a/49221353/1770003
The idea is, Android doesn't directly return the absolute path as other OS reports like Windows.这个想法是,Android 不会像其他操作系统报告(如 Windows)那样直接返回绝对路径。 A different approach is needed.
需要一种不同的方法。
The approach I took is,我采取的方法是,
In my case, I moved the caller into onRequestHandPathOz which is add by implementing "HandlePathOzListener.SingleUri".就我而言,我将调用者移动到 onRequestHandPathOz 中,这是通过实现“HandlePathOzListener.SingleUri”添加的。
@Override
public void onRequestHandlePathOz(PathOz pathOz, Throwable throwable)
{
txt_pathShow.setText(testGeneralEncryption(pathOz.getPath()));
}
The method "testGeneralEncryption" is defined in the original question and pathOz.getPath() is passed as an argument to the parameter "jstring myString".方法“testGeneralEncryption”在原始问题中定义,pathOz.getPath() 作为参数传递给参数“jstring myString”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.