簡體   English   中英

Android NDK:使用NDK + SDK android打開寫入SD卡的文件時出錯

[英]Android NDK: Error while opening a File written to SD card using NDK + SDK android

我是新來的Android和IAM試圖讀取使用NDK SD卡上的文件和發送讀取字節SDK,並將其寫入IAM使用SDK(檢查)的SD卡。我可以做sucefully從NDK打電話 - SDK與字節,我可以使用SDK將其寫入SD卡。。但是我無法打開寫入SD卡的文件。

我正在粘貼我的代碼

本機C文件

#include<stdio.h>
#include<string.h>
#include <jni.h> 
#include <stdlib.h> 
#include <android/log.h>  
#include "com_example_fileupload_NativeLib.h"

#define  LOG_TAG    "testjni" 
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)  

JavaVM *m_vm; 
jobject obj; 
jclass gFilePermInfoClass;
jmethodID gFilePermInfoClsConstructor;

jint JNI_OnLoad(JavaVM* vm, void* reserved) 
{
     JNIEnv* env;     
    LOGI("JNI INIT");      
    return JNI_VERSION_1_6; 

} 

JNIEXPORT jint JNICALL Java_com_example_fileupload_NativeLib_loop
  (JNIEnv *env, jobject obj)
{

LOGI("JNI work !"); 
jbyteArray data; 
jbyteArray data_rem; 
jclass clazz = (*env)->FindClass(env,"com/example/fileupload/NativeLib"); 
if (clazz == 0) { 
LOGI("FindClass error"); 
return 2; 
} 
gFilePermInfoClass = (*env)->NewGlobalRef(env, clazz);
LOGI("JNI before jmethodID"); 
jmethodID javamethod = (*env)->GetMethodID(env,gFilePermInfoClass, "callFromCPP", "([B)V"); 
gFilePermInfoClsConstructor = (*env)->NewGlobalRef(env, javamethod);
if (javamethod == 0) { 
LOGI("GetMethodID error"); 
return 3; 
} 
LOGI("JNI before calling java ");

LOGI(" before File "); 
FILE *fd = NULL;
    char buff[1*1024*1024];
char *h;
    unsigned int nread = 0;
     unsigned int nread_remaining = 0;
int count=0;
    memset(buff, '\0', sizeof(buff));
    fd = fopen("/sdcard/20MB.zip","r+");
    fseek ( fd, 0 , SEEK_END ); 
    int fileSize = ftell(fd); 
    LOGI("****SUDARSHAN C SIZE %d",fileSize);

    if(NULL == fd) // Check for error
    {
       LOGI("\n File opening failed\n");
        return 1;
    }
    nread = (1*1024*1024); // Hard code the number of elements to 50

    if(nread_remaining = fileSize%nread)
    {
    count = fileSize/nread;
    LOGI("***** IN IF rem %d",nread_remaining);
    }
    else{
    count = fileSize/nread;
    LOGI("***** IN ELSE COUNT  %d",count);
    }

 data = (*env)->NewByteArray(env, nread); 
  data_rem = (*env)->NewByteArray(env, nread_remaining); 
  if(data == NULL){         
  LOGI("No memory could be allocated for buffer");         
  return -1;     
  } 

LOGI("Before While in C "); 
int count_test=0;
int i;
   for(i=count;i>0;i--) //while(count_test) //while( fread(buff, 1, nread, fd) )
    {
        fread(buff, 1, nread, fd);
        LOGI("INSIUDE WHILE SUDARSHAN **** %d",i);
        (*env)->SetByteArrayRegion(env, data, 0, nread, buff);     
        (*env)->CallVoidMethod(env, obj, gFilePermInfoClsConstructor, data); 
    }

    if(nread_remaining){
    char buffer[nread_remaining];
    fread(buffer, 1, nread_remaining, fd);
    (*env)->SetByteArrayRegion(env, data_rem, 0, nread_remaining, buffer);     
    (*env)->CallVoidMethod(env, obj, gFilePermInfoClsConstructor, data_rem); 
    }

    if(feof(fd))
    {
       printf("\n Seems like end of file was reached\n");
    }
    else if(ferror(fd))
    {
       printf("\n Some errors on this stream occurred\n");
    }

    // Reset the buffer with NULLs
    memset(buff, '\0', sizeof(buff));
    return (10+11);
}

NativeLib.java

package com.example.fileupload;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Environment;



public class NativeLib {

    public int count=0;
    File someFile = null;
    FileOutputStream fos = null;
    File sdDir = new File(Environment.getExternalStorageDirectory().getPath());
    public NativeLib() throws FileNotFoundException{

        someFile = new File(sdDir.getPath() +"/22MB.zip");
        fos = new FileOutputStream(someFile);

        //fos.flush();fos.close();

    }
      static {
        System.loadLibrary("com_example_fileupload_NativeLib");
      }

      /** 
       * Adds two integers, returning their sum
       */
      public native int loop();

      /**
       * Returns Hello World string
     * @throws IOException 
       */
      //public native String hello();

      public void callFromCPP(byte[] buff)
        {  
          count=count+buff.length;
          try {
              //count=count+buff.length;
              System.out.println("************ Sudarshan Inside callfromcpp try "+count+"   "+buff.length);
            fos.write(buff);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("************ Sudarshan Inside callfromcpp catch "+e.toString());
            e.printStackTrace();
        }
         // count=count+buff.length;
          //System.out.println("************ Sudarshan Inside callfromcpp 1 "+buff.toString()+"   "+count); 

        } 

我叫本地人fn。 我的MainActivity.java和java fn中的loop() 來自本地的呼叫是callFromCPP(byte [] buff) ,此處僅將字節寫入本地返回的SD卡(名為22MB.zip )。

PLZ幫助

瑞格斯

迪帕克

我發現我做錯了..“ fread”函數讀取字節有一個限制。.我給了1MB(我的文件中的nread值)太大了。所以我將該值更改為100,然后有效

問候,

迪帕克

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM