Alain75
Member
Hi,
I am writing a simple jpg file compressor. Currently, I succeeded in :
And the log in B4A :
However the new file before setting metadata is well written :
Any help ?
Thanks !
I am writing a simple jpg file compressor. Currently, I succeeded in :
- getting the file,
- getting its metadata (GPS references and DateTimeDigitized) thanks to @Erel post on SimpleMediaManager
- writing the new file with parameterized compression level,
- setting the metada of the new file with ExifInterface method setAttribute
Extracted code:
Sub Process_Globals
Dim meta() As String = Array As String("GPSLatitudeRef","GPSLatitude","GPSLongitudeRef","GPSLongitude","DateTime","DateTimeDigitized")
...
Sub Copy
...
'f1 is internal image compressed
'f2 is name of the new file in the external storage directory where I choose the file
Dim in As InputStream = File.OpenInput(File.DirInternal,f1)
Dim out As OutputStream = sto.OpenOutputStream(sto.CreateNewFile(sto.Root,f2))
File.Copy2(in, out)
out.Close
' Setting metadata attributes of new file
Dim in As InputStream = sto.OpenInputStream(sto.FindFile(sto.Root,f2))
Dim exif As JavaObject
exif.InitializeNewInstance("android.media.ExifInterface", Array(in))
Dim v() As String = Regex.Split(";",metadata), i As Int = 0
For Each a As String In meta
exif.RunMethod("setAttribute",Array(a,v(i)))
i = i + 1
Next
' Save modifications...(supposed to create a new file and then replace the file)
exif.RunMethod("saveAttributes",Null)
And the log in B4A :
B4X:
copy : content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001.jpg -> temp
copy : new -> 0001-new.jpg
main_copy (java line: 509)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4a.jpgsizer.main._copy(main.java:509)
at b4a.jpgsizer.main._compressimage(main.java:444)
at b4a.jpgsizer.main._compressall(main.java:412)
at b4a.jpgsizer.main._save_click(main.java:800)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.io.IOException: Failed to save new file
at android.media.ExifInterface.saveAttributes(ExifInterface.java:2130)
... 23 more
Caused by: java.io.IOException: write failed: EBADF (Bad file descriptor)
at libcore.io.IoBridge.write(IoBridge.java:540)
at java.io.FileOutputStream.write(FileOutputStream.java:398)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:121)
at android.media.ExifInterface$ByteOrderedDataOutputStream.write(ExifInterface.java:4787)
at libcore.io.Streams.copy(Streams.java:203)
at android.media.ExifInterface.saveJpegAttributes(ExifInterface.java:3530)
at android.media.ExifInterface.saveAttributes(ExifInterface.java:2118)
... 23 more
Suppressed: java.io.IOException: write failed: EBADF (Bad file descriptor)
at libcore.io.IoBridge.write(IoBridge.java:540)
at java.io.FileOutputStream.write(FileOutputStream.java:398)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at java.io.FilterOutputStream.close(FilterOutputStream.java:179)
at android.media.ExifInterface.saveAttributes(ExifInterface.java:2115)
... 23 more
Caused by: android.system.ErrnoException: write failed: EBADF (Bad file descriptor)
at libcore.io.Linux.writeBytes(Native Method)
at libcore.io.Linux.write(Linux.java:293)
at libcore.io.ForwardingOs.write(ForwardingOs.java:240)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:418)
at libcore.io.ForwardingOs.write(ForwardingOs.java:240)
at libcore.io.IoBridge.write(IoBridge.java:535)
... 28 more
Caused by: android.system.ErrnoException: write failed: EBADF (Bad file descriptor)
at libcore.io.Linux.writeBytes(Native Method)
at libcore.io.Linux.write(Linux.java:293)
at libcore.io.ForwardingOs.write(ForwardingOs.java:240)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:418)
at libcore.io.ForwardingOs.write(ForwardingOs.java:240)
at libcore.io.IoBridge.write(IoBridge.java:535)
... 30 more
However the new file before setting metadata is well written :
Any help ?
Thanks !