Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:43:55+00:00 2026-06-10T14:43:55+00:00

I am new to android and iam trying to read a file from SD

  • 0

I am new to android and iam trying to read a file from SD card using NDK and sending that read bytes to SDK and iam writing it to the SD card using SDK (for checking).. I could sucefully make a call from NDK – SDK with the bytes and i could write it to the SD card using SDK.. But i coulnt open the file written to the sd card..

I am pasting my code

Native C file

#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); 

        } 

Iam calling the native fn. loop() from my MainActivity.java and the java fn. call from native is callFromCPP(byte[] buff) and here only iam writing the byte’s to SD card (named 22MB.zip) returned from native..

PLZ help

Reagards,

Deepak

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:43:56+00:00Added an answer on June 10, 2026 at 2:43 pm

    I found the mistake i had done,.. there is a limit for “fread” function to read bytes.. I had given 1MB (nread value in my c fle) which is too large.. So i changed the value to 100 and it worked

    Regards,

    Deepak

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read a file from /data partition in Android. I wrote
hi friends iam trying to upload a file from the android device to the
I am new to android..Actually iam trying to increase the RAM Memory of android
I am new in android. I am trying to learn Android. For that my
I am new to using SQLite databases and android mobile development. I am trying
I am new for android development. I am trying read and write contacts to
I'm trying to read in a text file of a bunch of words that
I am trying to stream files from a zip file using ZipEntry class in
I am new in Android, i want to fetch the video file from web
I am trying to read an html file from a url source, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.