[英]Copy file to photo library using C++ / NDK (Android Development)
[英]Open and read text file in c++ with Android (ndk)
我正在嘗試在Android中制作一個使用C ++中的OpenCV的應用程序。 我面臨的問題是我想從本機部分打開文本文件以逐字讀取它,但無法打開。 我的代碼如下:
JNI方法:
JNIEXPORT jboolean JNICALL Java_com_alejandro_nativeopencv_NativeClass_initRecognizer(JNIEnv * env, jclass clazz){
recognizer = Recognizer("ORB","ORB","BruteForce-Hamming");
recognizer.createObject("/storage/emulated/0/TFG/Fotos/Carpeta", true);
recognizer.createObject("/storage/emulated/0/TFG/Fotos/Cereales", true);
return true;
}
和我正在使用的Recognizer :: createObject方法:
Recognizer.cpp :
#include "headers/Recognizer.h"
#include <fstream>
#include <iostream>
#include <string>
#include <android/log.h>
using namespace std;
using namespace cv;
//...other methods, constructors and stuff...
Object Recognizer::createObject(String path, bool add) {
__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
ifstream file((path + "/info.txt").c_str());
if (!file.is_open()){
//always enters here
return Object(true); //null object
} else{
//never enters here
String name;
vector < vector<KeyPoint> > keypoints;
vector <Mat> descriptors;
vector < vector<Point2f> > corners;
vector <String> viewNames;
bool easy;
string word;
int i = 0;
while (file >> word)
{
if(i == 0){
/* Object name */
name = word;
__android_log_print(ANDROID_LOG_DEBUG,"NOMBRE","%s",name.c_str());
} else if(i == 1){
/* Object easy or not */
easy = (word == "true");
__android_log_print(ANDROID_LOG_DEBUG, "EASY", "%d", easy);
} else if(i%2 == 0){
/* Object image view*/
keypoints.push_back(vector <KeyPoint>());
descriptors.push_back(Mat());
corners.push_back(vector <Point2f>(4));
Mat image = imread(path + "/" + word, CV_LOAD_IMAGE_GRAYSCALE);
this->detector->detect(image, keypoints[(i/2)-1]);
this->extractor->compute(image, keypoints[(i/2)-1], descriptors[(i/2)-1]);
corners[(i/2)-1][0] = cvPoint(0,0);
corners[(i/2)-1][1] = cvPoint(image.cols,0);
corners[(i/2)-1][2] = cvPoint(image.cols, image.rows);
corners[(i/2)-1][3] = cvPoint(0, image.rows);
__android_log_print(ANDROID_LOG_DEBUG, "VISTA", "%d", (i/2)-1);
} else{
/* Object name view */
viewNames.push_back(word);
String aux = word;
__android_log_print(ANDROID_LOG_DEBUG, "VISTA NOMBRE", "%s", aux.c_str());
}
i++;
}
Object obj = Object(name, keypoints, descriptors, corners, viewNames, easy);
if(add){
this->objects.push_back(obj);
}
file.close();
return obj;
}
}
問題在這里:
__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
ifstream file((path + "/info.txt").c_str());
if (!file.is_open()){
//always enters here
return Object(true); //null object
} else{
//never enters here
...do stuff...
}
在JNI方法中,我將要讀取的info.txt文件所在的路徑作為參數傳遞,但沒有打開它(沒有錯誤,它只輸入第一個if語句。我首先嘗試將我的手機sdcard中的文件夾,然后在內部存儲中,但是它不能以任何方式工作。我檢入了手機,我猜路徑是正確的:
另外,我在Android Manifest中具有讀寫外部存儲權限。
您知道這里的問題是什么?
非常感謝你!
編輯1:
將代碼更改為此:
__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
ifstream file;
file.open((path + "/info.txt").c_str());
if (!file.is_open()){
//always enters here
return Object(true); //null object
} else{
//never enters here
...do stuff...
}
仍然有同樣的問題。
編輯2
我得到的日志結果是這些(它們與它嘗試訪問的路徑相對應):
D/path: /storage/emulated/0/TFG/Fotos/Carpeta/info.txt
D/path: /storage/emulated/0/TFG/Fotos/Cereales/info.txt
根據我的設備,文件info.txt在該路徑中:
該file
尚未打開,您必須在is_open
檢查之前調用file.open()
函數將流與當前文件關聯。
file.open();
if (!file.is_open()){
return Object(true);
}
更新:另一個原因可能是您沒有運行時權限模型。 嘗試官方文檔和此鏈接以獲取准確的示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.