2016-09-29 171 views
2

我想在使用C++中的OpenCV的Android中創建應用程序。我面對的問題是我想從本地打開一個文本文件逐字閱讀,但它不會打開。我的代碼如下:在Android中使用C++打開和讀取文本文件(ndk)

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; 
} 

而且識別器:: 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語句。我首先嚐試將文件夾放入手機的SD卡中,然後在我的內部存儲器中嘗試,但無法以任何方式工作。我在電話檢查,我猜的路徑是正確的:

Link to the image showing the folder path

我也有在Android清單讀取和寫入外部存儲權限。

你知道這裏的問題在哪?

非常感謝!

EDIT 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... 
} 

仍然具有同樣的問題。

EDIT 2

日誌結果我得到的這些(它們與對應的路徑它試圖訪問):

D/path: /storage/emulated/0/TFG/Fotos/Carpeta/info.txt 
D/path: /storage/emulated/0/TFG/Fotos/Cereales/info.txt 

的info.txt文件是在這條道路根據我的設備:

Photo

+0

附加您正在使用的日誌詳細信息,並嘗試使用此方法http:// stackoverflow。com/questions/20663467/jni-reading-a-text-file-in-c-code-and-returning-to-sdk –

+0

你的android手機的android版本是什麼?因爲如果它大於棒棒糖比確保你已經實施了權限模型 –

+0

@PavneetSingh添加了日誌的詳細信息。將嘗試你發佈的方法並告訴你,謝謝:) PS:Android版本是6.0.1(棉花糖) – AMarquez94

回答

2

file尚未打開,你哈已經調用file.open()函數將is_open檢查前的流與當前文件相關聯。

file.open(); 
if (!file.is_open()){ 
     return Object(true); 
    } 

更新:另一個原因可能是因爲你沒有運行權限模型。嘗試使用official docsthis link for precise example

+1

工作像一個魅力,非常感謝你! (我贊成你的回答,但它表示不會顯示,因爲我的對位點不到15個,對不起)。 – AMarquez94