[SOLVED]ActivityForResult in Jcamera --Crash
lazarus / 43-Android
----------------------------------------------------------------------
Guser979 · 2020-09-07 11:41
I tried several ways to resolve this but I can't.
This is Jcamera, which uses intents to capture the image with the
device's camera.
The crash app right on this line in controls.java:
this.activity.startActivityForResult (intent, requestCode);
If anyone can help. grateful.
public String jCamera_takePhoto(String path, String filename, int
requestCode) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//String image_path = (path+File.separator+filename);
File newfile = new File(path, File.separator+filename);
File dirAsFile = newfile.getParentFile();
if (!dirAsFile.exists()) {
dirAsFile.mkdirs();
}
try {
newfile.createNewFile();
}
catch (IOException e) {
Log.e("File creation error",newfile.getPath());
}
Uri uri =
jSupported.FileProviderGetUriForFile(this.GetContext(), newfile);
if (jSupported.IsAppSupportedProject()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//outputFileUri
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
else {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
uri); //mImageCaptureUri
intent.putExtra("return-data", true);
}
if
(intent.resolveActivity(this.GetContext().getPackageManager()) !=
null) {
ERROR>>>>>> this.activity.startActivityForResult(intent,
requestCode);
}
galleryAddPic(newfile);
return newfile.toString();
}
----------------------------------------------------------------------
devEric69 · 2020-09-07 12:16
Hello (btw, it is a development site, talking about Pascal Object
programming ),
Nevertheless, the jCamera_takePhoto routine makes a call to " this ".
==> afaik, " this " should be an instance of an object that contains
the jCamera_takePhoto subroutine: does Java support subroutines (handy
to hide a routine, not even visible in Private section, i.e. to
lighten a Class signature, amo)?
==> if Java does not support subroutines, then you have to pass " this
" as a parameter to jCamera_takePhoto eg:
Code: Java [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i public
String jCamera_takePhoto(Camera aCamera, String path, String filename,
int requestCode) {
...\... aCamera.activity.startActivityForResult(intent, requestCode);
...\... } [/s]
Sorry (my answer is irrelevant): I have no skills neither in
Laz4Android, nor in LAMW .
----------------------------------------------------------------------
jmpessoa · 2020-09-07 17:56
Hi, Guser979!
Please, update [and test] "jBitmap.java" from github....
[the error is in file save task ...]
----------------------------------------------------------------------
Guser979 · 2020-09-07 19:26
Hi, I tried... after tests and rebuilds ... same error. on the same
line in controls.java
----------------------------------------------------------------------
jmpessoa · 2020-09-07 20:14
Ok.
Can you test "AppCameraDemo"
[after update jBitmap.java in LAMW folder
"......\android_wizard\smartdesigner\java" ]
----------------------------------------------------------------------
Guser979 · 2020-09-07 20:29
of course...but tests also included the demo.
on the other hand: I tried JintentManager using iaImageCapture.
And PutExtraMediaStoreOutput ... The crash also occurs just when
StartActivityforresult is called.
So I made a change to the following line of code in
Jintentmanager.java
public void PutExtraMediaStoreOutput (String
_environmentDirectoryPath, String _fileName) {
mIntent.putExtra (MediaStore.EXTRA_OUTPUT, Uri.parse ("content: //" +
_environmentDirectoryPath + "/" + _fileName));
}
Note: With the change The crash does not occur on the emulator
(Android 6), but on the device (Android 9) crash and resultcode is
RESULT_CANCELED after take photo
original code in jintentmanager is .... Uri.parse ("file: //" ....
----------------------------------------------------------------------
jmpessoa · 2020-09-07 20:34
My test [AppCameraDemo"] is OK in in Android 4.3 and 5.01 and
6.01.... [after updated jBitmap.java]
Can you put some ADB Logcat?
----------------------------------------------------------------------
Guser979 · 2020-09-07 20:37
Yes. As soon as I can make it work. Not for now. Once resolved, I will
do that. Thank you Jm.
EDIT :
Can this help?
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: FATAL
EXCEPTION: main
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: Process:
org.lamw.appcamerademo2, PID: 13671
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime:
android.os.FileUriExposedException:
file:///storage/emulated/0/DCIM/photo1.jpg exposed beyond app through
ClipData.Item.getUri()
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.net.Uri.checkFileUriExposed(Uri.java:2371)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.content.ClipData.prepareToLeaveProcess(ClipData.java:966)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.content.Intent.prepareToLeaveProcess(Intent.java:10947)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.content.Intent.prepareToLeaveProcess(Intent.java:10932)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at android.a
pp.Instrumentation.execStartActivity(Instrumentation.java:1671)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.app.Activity.startActivityForResult(Activity.java:4703)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at android.s
upport.v4.app.FragmentActivity.startActivityForResult(FragmentActivity
.java:767)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.app.Activity.startActivityForResult(Activity.java:4661)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at android.s
upport.v4.app.FragmentActivity.startActivityForResult(FragmentActivity
.java:754)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
org.lamw.appcamerademo2.Controls.jCamera_takePhoto(Controls.java:2836)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
org.lamw.appcamerademo2.Controls.pOnClick(Native Method)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
org.lamw.appcamerademo2.jButton$1.onClick(jButton.java:80)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.view.View.performClick(View.java:7357)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.widget.TextView.performClick(TextView.java:14263)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.view.View.performClickInternal(View.java:7323)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.view.View.access$3200(View.java:849)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.view.View$PerformClick.run(View.java:27895)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.os.Handler.handleCallback(Handler.java:873)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.os.Handler.dispatchMessage(Handler.java:99)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.os.Looper.loop(Looper.java:216)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
android.app.ActivityThread.main(ActivityThread.java:7266)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
java.lang.reflect.Method.invoke(Native Method)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at com.andro
id.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:49
4)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
----------------------------------------------------------------------
jmpessoa · 2020-09-07 21:43
QuoteYes. As soon as I can make it work.
Well, we need ADB Logcat to find/trace the error!
"Tools" --> "[LAMW]..." ---> "ADB Logcat" ---> "....[dump]"
----------------------------------------------------------------------
Guser979 · 2020-09-07 21:55
Yes. But I'm without the emulator for now. So I'm just using the
device
Edit SOLVED
In Controls.java add
import android.os.StrictMode;
and ...
in Camera (controls.java)
public String jCamera_takePhoto(String path, String filename, int
requestCode) {
ADD >>>> StrictMode.VmPolicy.Builder builder = new
StrictMode.VmPolicy.Builder();
ADD >>> StrictMode.setVmPolicy(builder.build());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//String image_path = (path+File.separator+filename);
File newfile = new File(path, File.separator+filename);
File dirAsFile = newfile.getParentFile();
if (!dirAsFile.exists()) {
dirAsFile.mkdirs();
}
try {
newfile.createNewFile();
}
catch (IOException e) {
Log.e("File creation error",newfile.getPath());
}
Uri uri =
jSupported.FileProviderGetUriForFile(this.GetContext(), newfile);
if (jSupported.IsAppSupportedProject()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//outputFileUri
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
else {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
uri); //mImageCaptureUri
intent.putExtra("return-data", true);
}
if
(intent.resolveActivity(this.GetContext().getPackageManager()) !=
null) {
this.activity.startActivityForResult(intent, requestCode);
//this.activity.startActivityForResult(intent, requestCode);
}
galleryAddPic(newfile);
return newfile.toString();
}
Result : Working in android 9 (DEvice)
Thank´s JM
https://stackoverflow.com/questions/48117511/exposed-beyond-app-
through-clipdata-item-geturi/48851566#48851566
Note: This works. It does not mean that it is the correct way to
resolve.
----------------------------------------------------------------------
jmpessoa · 2020-09-08 01:06
Ok, we have some solution.... and this is good!
Can you do some test on another direction [ a new project ] ?
When Creating a new project:
Use "gradle" and
check the box "Add Support Library"
Work?
----------------------------------------------------------------------
Guser979 · 2020-09-08 02:00
It didn't work, JM. Only success without Add Support ...
All my projects, I build in Gradle. But I had never used Add Support
...
----------------------------------------------------------------------
jmpessoa · 2020-09-08 02:35
QuoteBut I had never used Add Support ...
When we check "Add Support" LAMW do some trick:
Copy the file "Supported.java" from folder
"....\android_wizard\smartdesigner\java\support"
that support "FileProvider" [ need by devices Api >= 24 ]
----------------------------------------------------------------------
Guser979 · 2020-09-08 02:50
Well. Checking my projects, this file is present in them in src> org>
lamw ... Even though I haven't used Add Suport
ops.... only the names are the same ... the contents are not
. I will do tests
tests done: Even "forcing" jSupported.java present in java / support,
I was not successful.
ps : and ... Even with modification found, it didn't work. So, I had
to go back to jsupported.java which is already in src ....
Grateful for the help. Tomorrow I can continue with the tests.
----------------------------------------------------------------------
jmpessoa · 2020-09-08 04:02
QuoteEven with modification found, it didn't work. So, I had to go
back to jsupported.java which is already in src ....
When we check "Add Support" LAMW do some trick:
[I forgot that there are, actually, two tricks... sorry]
1) Copy the file "Supported.java" from folder
"....\android_wizard\smartdesigner\java\support"
that support "FileProvider" [ need by devices Api >= 24 ]
2) Insert a new line in "build.gradle" [file] dependencies:
dependencies {
.......................
implementation 'com.android.support:support-v4:28.+' //
<<--------- NEW LINE!
}
But, now I will modifies LAMW form wizard....
if user select "gradle" then the "Add Supported.java" will be
checked!
[that already happens when the theme is "AppCompat...."]
----------------------------------------------------------------------
Guser979 · 2020-09-08 19:01
Hi, Jm.
Even with the changes and tips I get error when starting activity:
The error has changed and is now:
09-08 14:22:56.611 10481 15385 15385 E AndroidRuntime: FATAL
EXCEPTION: main
09-08 14:22:56.611 10481 15385 15385 E AndroidRuntime: Process:
org.lamw.appajustscreen, PID: 15385
09-08 14:22:56.611 10481 15385 15385 E AndroidRuntime:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.XmlResourceParser android.content.pm.ProviderInfo
.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)'
on a null object reference
09-08 14:22:56.611 10481 15385 15385 E AndroidRuntime: at android.s
upport.v4.content.FileProvider.parsePathStrategy(FileProvider.java:605
)
09-08 14:22:56.611 10481 15385 15385 E AndroidRuntime: at android.s
upport.v4.content.FileProvider.getPathStrategy(FileProvider.java:579)
09-08 14:22:56.611 10481 15385 15385 E AndroidRuntime: at android.s
upport.v4.content.FileProvider.getUriForFile(FileProvider.java:417)
09-08 14:22:56.611 10481 15385 15385 E AndroidRuntime: at org.lamw.
appajustscreen.jSupported.FileProviderGetUriForFile(jSupported.java:30
)
Note: As in the previous situation ...
Using (In Controls.java)
import android.os.StrictMode;
And adding this just before trigger the camera, it works :
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder
();
StrictMode.setVmPolicy (builder.build ());
i've tried to insert Authorithies, create providerpath.xml and other
tips obtained by searching, but without success so far.
Could not edit manifest. Lawm does not allow for example to edit
this line
I can't see what could be wrong in Lawm. It looks ok, according to
what I researched.
I know almost nothing about java but it seems to be ok.
So I don't really understand. PS: Manifest was not allowing editing
because I had a jprovider component in the project.
I removed it and was able to add authorithies. But it still doesn't
work.
controls.java
..............
............
Uri uri = jSupported.FileProviderGetUriForFile(this.GetContext(),
newfile);
if (jSupported.IsAppSupportedProject()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//outputFileUri
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
else {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
uri); //mImageCaptureUri
intent.putExtra("return-data", true);
}
.........
.........
.........
jSupported.java
package org.lamw.appsupportdemo5;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import java.io.File;
public class jSupported {
public static Uri FileProviderGetUriForFile(Controls controls, File
file) {
Uri r = null;
if (Build.VERSION.SDK_INT >= 24) {
//[ifdef_api24up]
r = FileProvider.getUriForFile(controls.GetContext(),
controls.GetContext().getApplicationContext().getPackageName() +
".provider", file);
//[endif_api24up]
}
else {
r = Uri.fromFile(file);
}
return r;
}
public static Uri FileProviderGetUriForFile(Context context, File
file) {
Uri r = null;
if (Build.VERSION.SDK_INT >= 24) {
//[ifdef_api24up]
r = FileProvider.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".provider", file);
//[endif_api24up]
}
else {
r = Uri.fromFile(file);
}
return r;
}
public static boolean IsAppSupportedProject() {
return true;
}
}
----------------------------------------------------------------------
jmpessoa · 2020-09-09 20:02
Please, put here your "providerpath.xml" ...
Thanks!
----------------------------------------------------------------------
Guser979 · 2020-09-09 21:35
I'm currently using this
----------------------------------------------------------------------
440bx · 2020-09-09 22:23
@Guser979,
Consider putting the [SOLVED] at the beginning of the thread title
instead of the end because at the end of it, it is often not visible.
HTH.
----------------------------------------------------------------------
jmpessoa · 2020-09-09 22:23
Ok.
Meanwhile, I commited the "StrictMode" solution....
You put "providerpath.xm" and this add on in manifest?
Quote
...................
----------------------------------------------------------------------
Guser979 · 2020-09-09 22:36
Almost
My
Note: In this case it is not the demo camera I am using. But the
errors are the same.
And my res xml is provider_paths.xml ...
When compiling with
I get
> org.xml.sax.SAXParseException; systemId:AndroidManifest.xml;
lineNumber: 56; columnNumber: 51; The element type "meta-data" must be
followed by the attribute specifications, ">" or "/>".
----------------------------------------------------------------------
jmpessoa · 2020-09-10 02:05
My suggestion [not add meta-data]
----------------------------------------------------------------------
Guser979 · 2020-09-10 02:32
Done.
9-09 23:22:44.953 10482 8758 8758 E AndroidRuntime: FATAL
EXCEPTION: main
09-09 23:22:44.953 10482 8758 8758 E AndroidRuntime: Process:
org.lamw.appajustscreen, PID: 8758
09-09 23:22:44.953 10482 8758 8758 E AndroidRuntime:
java.lang.RuntimeException: Unable to get provider
android.support.v4.content.FileProvider:
java.lang.IllegalArgumentException: Missing
android.support.FILE_PROVIDER_PATHS meta-data
And returning with Meta Data
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: FATAL
EXCEPTION: main
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: Process:
org.lamw.appajustscreen, PID: 15762
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime:
android.os.FileUriExposedException:
file:///storage/emulated/0/DCIM/12345%4081.jpg exposed beyond app
through ClipData.Item.getUri()
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.net.Uri.checkFileUriExposed(Uri.java:2371)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.content.ClipData.prepareToLeaveProcess(ClipData.java:966)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.content.Intent.prepareToLeaveProcess(Intent.java:10947)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.content.Intent.prepareToLeaveProcess(Intent.java:10932)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at android.a
pp.Instrumentation.execStartActivity(Instrumentation.java:1671)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.app.Activity.startActivityForResult(Activity.java:4703)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at android.s
upport.v4.app.FragmentActivity.startActivityForResult(FragmentActivity
.java:767)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.app.Activity.startActivityForResult(Activity.java:4661)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at android.s
upport.v4.app.FragmentActivity.startActivityForResult(FragmentActivity
.java:754)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
org.lamw.appajustscreen.Controls.jCamera_takePhoto(Controls.java:2841)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
org.lamw.appajustscreen.Controls.pOnClick(Native Method)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
org.lamw.appajustscreen.jButton$1.onClick(jButton.java:80)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.view.View.performClick(View.java:7357)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.widget.TextView.performClick(TextView.java:14263)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.view.View.performClickInternal(View.java:7323)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.view.View.access$3200(View.java:849)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.view.View$PerformClick.run(View.java:27895)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.os.Handler.handleCallback(Handler.java:873)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.os.Handler.dispatchMessage(Handler.java:99)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.os.Looper.loop(Looper.java:216)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.app.ActivityThread.main(ActivityThread.java:7266)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
java.lang.reflect.Method.invoke(Native Method)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at com.andro
id.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:49
4)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
----------------------------------------------------------------------
jmpessoa · 2020-09-10 17:29
QuoteAnd returning with Meta Data
Quote09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
09-09 23:36:50.466 10482 15762 15762 E AndroidRuntime: at
android.net.Uri.checkFileUriExposed(Uri.java:2371)
Using "StrictMode"?
----------------------------------------------------------------------
Guser979 · 2020-09-10 18:22
no "Strict mode" lines.
UPDATE :
jCamera is finally working perfectly on my Android 9 device. NO
Strictmode.
I had written before that I couldn't understand the error, since the
code in Lawm seemed to me within what the official Android
documentation points to. In fact, after countless tests and rebuilds,
I decided to delete the android_wizard in lawm folder.
I Extract the original "android_wizard" folder to the correct
location and did what JM indicated:
I copied jSupported.java from lazandroidmodulewizard-master \
android_wizard \ smartdesigner \ java \ support \ to
lazandroidmodulewizard-master\ android_wizard \ smartdesigner \ java
\.
Then I opened the appcamerademo and did the conversion to use gladde:
Tools \ [Lawm] Android Module Wizard \ Convert the project to
appcompat Theme.
Next :
added in Manifest
in created in res \ xml the file provider_paths.xml
Finally: Build Android Apk and Run (in device).
That is all.
Thanks to Jm and everyone who allows us to use Lazarus and Android.
PS :
My "config"
android-ndk-r10e-windows-x86
android-sdk_r24.4.1-windows
gradle-6.5-all
jdk-8u251-windows-x64.exe
JavaSetup8u251.exe
Important :
Everything is working as expected now. When starting New Project with
"add support", jSupported.java is automatically copied and
{
............. ..........
implementation com.android.support:support-v4:28.+'
}.....
So,only add provider needed in manifest and add provider_paths.
----------------------------------------------------------------------
jmpessoa · 2020-09-13 18:48
QuoteSo,only add provider needed in manifest and add provider_paths.
Ok. I will improve the LAMW wizard [to add the missing xml ...] and
drop "StrictMode" code!
Thank you!!!!