fread from asset

3,089 views
Skip to first unread message

yaggy

unread,
Oct 14, 2011, 8:39:19 AM10/14/11
to android-ndk
I wonder if it's possible to fread(and f-thing stdio functions) files
in assets folder of apk.
The code below always lose response.

#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
void myLoadAsset(AAssetManager* mngr){
....
const char imageFilename[] = "image.png";
AAsset* asset = AAssetManager_open(mngr, imageFilename,
AASSET_MODE_UNKNOWN);
FILE* fp = (FILE*)AAsset_getBuffer(asset);
size_t imageSize = AAsset_getRemainingLength(asset);
unsigned char *pImage = (unsigned char*)malloc(sizeof(unsigned
char) * imageSize);
AAsset_seek(asset, 0, SEEK_SET); // or fseek(fp, 0, SEEK_SET);
fread(pImage, 1, imageSize, fp);
....
}

I changed AAsset_seek to fseek but the result was the same.
I've already checked pImage, imageSize and fp have valid values.

Krystian Bigaj

unread,
Oct 15, 2011, 5:05:27 AM10/15/11
to android-ndk
AAsset_getBuffer gives you direct pointer to your data in memory, not
a FILE*.

You could use only AAsset_openFileDescriptor() (with seek,read), but
your assets must be not compressed (which for now I see depends on
file extension, google for kNoCompressExt).
Also when using AAsset_openFileDescriptor keep in mind that it gives
you handle/fd to opened .apk file, so seek() is not relative to opened
file, but to .apk file.

Personally I would stick to AAsset_getBuffer+AAsset_getLength (you get
pointer to your whole file data in memory, or as a mmap), or
AAsset_read+AAsset_seek+AAsset_getLength+...

yaggy

unread,
Oct 15, 2011, 7:23:22 AM10/15/11
to android-ndk
Thank you for your commnet, Krystian.
Now I understand asset manager is not a bridge to FILE, but totally
another way to handle assets.

My purpose is to load PNG format data with libpng. I'll try it with
mapped image on memory.

yaggy

Ignacio Vela

unread,
Oct 15, 2011, 10:15:32 AM10/15/11
to andro...@googlegroups.com
I need to open a file (avi) in ndk.

I can't figure how can I have access to this file that should be in my application files. Can please somebody give an example?

I've read that if I change the extension to mpg the file won't be compressed, but in this case how can I get the file path to open it in C?

Thanks!

Zoran Angelov

unread,
Oct 15, 2011, 8:47:36 PM10/15/11
to andro...@googlegroups.com
Hi,

And after you get native file descriptor you can change read function of libpng's png_structp with calling png_set_read_fn .


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Zoran Angelov

unread,
Oct 17, 2011, 10:38:52 AM10/17/11
to andro...@googlegroups.com
If you place the file in res/raw it won't be compressed.
For reading it with native C file-descriptor functions read the the message-thread link posted previously.
I'm using that method for opening multimedia files with ffmpeg and it works great.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/aNCG6uDFJLIJ.

Crossle Song

unread,
Dec 17, 2013, 4:32:39 AM12/17/13
to andro...@googlegroups.com
I use this code, but can not open file in FFmpeg, can you tell me How to open asset video mp4 file in FFmpeg
 
   AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
    const char *fname = (*env)->GetStringUTFChars(env, name, NULL);
    AAsset *asset = AAssetManager_open(mgr, fname, AASSET_MODE_STREAMING);
  (*env)->ReleaseStringUTFChars(env, name, fname);
    off64_t start, length;
    int fd = AAsset_openFileDescriptor64(asset, &start, &length);
    AAsset_close(asset);

   char str[128];
   snprintf(str, 128, "pipe:%d", fd); // fd:

emymrin

unread,
Dec 18, 2013, 10:07:38 AM12/18/13
to andro...@googlegroups.com
@Crossle Song
You can do that by implementing and registering your own URLProtocol.

Alex Cohn

unread,
Jul 14, 2014, 6:41:49 PM7/14/14
to andro...@googlegroups.com
On Tuesday, December 17, 2013 11:32:39 AM UTC+2, Crossle Song wrote:
I use this code, but can not open file in FFmpeg, can you tell me How to open asset video mp4 file in FFmpeg
 
   AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
    const char *fname = (*env)->GetStringUTFChars(env, name, NULL);
    AAsset *asset = AAssetManager_open(mgr, fname, AASSET_MODE_STREAMING);
  (*env)->ReleaseStringUTFChars(env, name, fname);
    off64_t start, length;
    int fd = AAsset_openFileDescriptor64(asset, &start, &length);
    AAsset_close(asset);

   char str[128];
   snprintf(str, 128, "pipe:%d", fd); // fd:

See How to properly pass an asset FileDescriptor to FFmpeg using JNI in Android. TL;NR: ffmpeg pipe does not know about your start; luckily, you can initialize AVFormatContext with non-zero skip_initial_bytes and iformat.

BR,
Alex Cohn
Reply all
Reply to author
Forward
0 new messages