I'm trying to read the Exif data of an image, in Nativescript. I rely on a native Java code i saw in a tutorial:
Uri uri; // the URI you've received from the other app
InputStream in;
try {
in = getContentResolver().openInputStream(uri);
ExifInterface exifInterface = new ExifInterface(in);
// Now you can extract any Exif tag you want
// Assuming the image is a JPEG or supported raw format
} catch (IOException e) {
// Handle any errors
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
This is my attempt to implement it with JS:
import * as application from "application";
getExif(src){//The src is a path to a file in the camera folder
const contentResolver = application.android.nativeApp.getContentResolver();
const stream = contentResolver.openInputStream(src);
const exif = new android.support.media.ExifInterface(st)//This throws an error
}
I get this error:
Error: JNI Exception occurred (SIGABRT). ======= Check the 'adb
logcat' for additional information about the error
What is the problem with my code?
Update:
app.gradle:
dependencies {
// implementation 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:exifinterface:25.1.0'
}
It now actually seems to me that the error is thrown before i even try to use the plugin. It actually fails here:
const contentResolver = application.android.nativeApp.getContentResolver();
const st = contentResolver.openInputStream(src);//FAIL
I get the error i mentioned initially.
Related
Here I try to use getClassLoader().getResources() to get my .model file, however it returns null. I'm not sure where goes wrong!
And when I try to print out the urls, it gives me java.lang.TwoEnumerationsInOne#5fd1900, what does this means?
public Activity(MainActivity activity) {
MainActivity activity = new MainActivity();
try {
// Open stream to read trained model from file
InputStream is = null;
// this .model file is save under my /project/app/src/main/res/
Enumeration<URL> urls = Activity.class.getClassLoader().getResources("file.model");
// System.out.println("url:"+urls);
if (urls.hasMoreElements()) {
URL element = urls.nextElement();
is = element.openStream();
}
// deserialize the model
classifier = (J48) SerializationHelper.read(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
In Android, resources put under src/main/res are not visible in the class path and can only be accessed via the android resources API. Try to put the file into src/main/resources.
I'm developing phonegap app and right now I'm trying to write a plugin to convert .amr files to .mp3 files. I'm using JAVE to do this conversion and while it's working on desktop it fails on android with this exception:
java.io.IOException: Error running exec().
Command: [/data/data/<my_package>/cache/jave-1/ffmpeg, -i, /sdcard/<my_filename>.amr, -vn, -acodec, libmp3lame, -f, mp3, -y, /sdcard/<my_filename>.mp3]
Working Directory: null Enviroment: null
I'm trying to do conversion like this:
private void convert(String input_file, CallbackContext callbackContext){
File input = new File(input_file);
File output = new File(input_file.replace(".amr", ".mp3"));
Encoder encoder = new Encoder();
EncodingAttributes encodingAttributes = new EncodingAttributes();
AudioAttributes audioAttributes = new AudioAttributes();
audioAttributes.setCodec("libmp3lame");
encodingAttributes.setAudioAttributes(audioAttributes);
encodingAttributes.setFormat("mp3");
try{
encoder.encode(input, output, encodingAttributes);
input.delete();
callbackContext.success("finished");
}
catch(Exception e){
callbackContext.error(e.getMessage());
}
}
I found this answer and I understand why error happens but the answer doesn't provide any solution. Is there a way to get this working in Phonegap project? Do I need to package ffmpeg library together with plugin and copy it to correct folder when app invokes the plugin?
/data/data/<my_package>/cache/jave-1
Stucked with same problem. For now i found that there is problem with file ffmpeg permissions. JAVE trying to set them, but for some reason it not working.
So i set this permission by myself like this:
Process process = null;
DataOutputStream dataOutputStream = null;
try {
process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
dataOutputStream.writeBytes("chmod 0755 __AppCachePath__/jave-1/ffmpeg\n");
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
} catch (Exception e) {
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
process.destroy();
} catch (Exception e) {
}
}
After this File permission problem goes away.
Unfortunatelly, I haven't found any documentation on this point, so maybe I do something wrong.
I use mupdf sample for android and I have slightly modified the source code of MuPDFActivity this way:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAlertBuilder = new AlertDialog.Builder(this);
if (core == null) {
core = (MuPDFCore)getLastNonConfigurationInstance();
if (savedInstanceState != null && savedInstanceState.containsKey("FileName")) {
mFileName = savedInstanceState.getString("FileName");
}
}
if (core == null) {
Intent intent = getIntent();
byte buffer[] = null;
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
try {
InputStream inputStream = new FileInputStream(new File(uri.toString()));
int len = inputStream.available();
buffer = new byte[len];
inputStream.read(buffer, 0, len);
inputStream.close();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
if (buffer != null) {
core = openBuffer(buffer);
} else {
core = openFile(Uri.decode(uri.getEncodedPath()));
}
SearchTaskResult.set(null);
}
The activity's openBuffer() method hasn't been changed:
private MuPDFCore openBuffer(byte buffer[]) {
System.out.println("Trying to open byte buffer");
try {
core = new MuPDFCore(this, buffer);
// New file: drop the old outline data
OutlineActivityData.set(null);
} catch (Exception e) {
System.out.println(e);
return null;
}
return core;
}
After I try to open any pdf, app shows alert with message "Cannot open document". This is logs:
03-21 10:43:14.754 3601-3601/com.artifex.mupdfdemo.debug E/libmupdf﹕ Opening document...
03-21 10:43:14.754 3601-3601/com.artifex.mupdfdemo.debug E/libmupdf﹕ error: No document handlers registered
03-21 10:43:14.754 3601-3601/com.artifex.mupdfdemo.debug E/libmupdf﹕ error: Cannot open memory document
03-21 10:43:14.754 3601-3601/com.artifex.mupdfdemo.debug E/libmupdf﹕ Failed: Cannot open memory document
So, whether there is any way to open pdf file as a byte array and what is this way?
My guess is that you are using a version of the android jni file mupdf.c that is older than the version of the main library files. There was a change in the library API which necessitates fz_register_document_handlers(ctx) being called between creating the context and calling fz_open_document. The first error message you are seeing suggests that fz_register_document_handlers is not being called
I can download the original file from Google Drive by using the following code :
public static InputStream downloadFile(Drive service, File file)
throws IOException {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp = service.getRequestFactory()
.buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
But it looks like the connection really low when downloading many files to add into the grid view.
Therefore, I need to list thumb nail instead of original file as it will be better for the connection.
Please help me how?
Use File.getThumbnail method to get the thumbnail or File.getIconLink to get the icon link.
http://www.isco.com/webproductimages/appBnr/bnr1.jpg
I've used a website to see the metadata of a image. In this website, it shows all info of image. I want to know how to get the "Title" tag of above image in android.
I found here the similiar question for iOS only: How to get image metadata in ios
However I don't know how to get "Meta data" of image on android. ExifInterface only gives some informations. But I'm unable to get "Title" tag with it.
Can you provide any code snippet for get meta data in android for image?
Download metadata extractor from the link given here ...... click to download the library
choose the version 2.5.0.RC-3.zip
Extract the jar Folder
and import jar into libs folder in your poject and then execute the below code
try {
InputStream is = new URL("your image url").openStream();
BufferedInputStream bis = new BufferedInputStream(is);
Metadata metadata = ImageMetadataReader.readMetadata(bis,true);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
}
}
catch (ImageProcessingException e){}
catch (IOException e) {}
If you want to retrieve meta data information about an image ExifInterface is what you are looking for. Here is a quite good example of how this interface is used: http://android-er.blogspot.com/2009/12/read-exif-information-in-jpeg-file.html
But if you want to retrieve information of an online image I'm afraid it's not yet possible.
If you want to retrieve metadata from image in Android project, then you can do that with the help of: https://github.com/drewnoakes/metadata-extractor
Implement this in your gradle using
implementation 'com.drewnoakes:metadata-extractor:2.12.0'
Complete code is as follows
private static String getImageMetaData(Uri image1) {
try {
InputStream inputStream = FILE_CONTEXT.getContentResolver().openInputStream(image1);
try {
image1.getEncodedUserInfo();
Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
for (Directory directory1 : metadata.getDirectories()) {
if (directory1.getName().equals("Exif IFD0")) {
for (Tag tag : directory1.getTags()) {
if (tag.getTagName().equals("Date/Time")) {
return tag.getDescription();
}
}
}
}
} catch (ImageProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}