Recent

Author Topic: [SOLVED]ActivityForResult in Jcamera --Crash  (Read 1433 times)

Guser979

  • New Member
  • *
  • Posts: 21
[SOLVED]ActivityForResult in Jcamera --Crash
« on: September 07, 2020, 01:41:52 pm »
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();
    
}
« Last Edit: September 10, 2020, 12:58:39 am by Guser979 »

devEric69

  • Sr. Member
  • ****
  • Posts: 353
Re: ActivityForResult in Jcamera --Crash
« Reply #1 on: September 07, 2020, 02:16:00 pm »
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][+][-]
  1. public String jCamera_takePhoto(Camera aCamera, String path, String filename, int requestCode) {
  2.   ...\...
  3.  aCamera.activity.startActivityForResult(intent, requestCode);
  4.   ...\...
  5.  }
[/s]

Sorry (my answer is irrelevant): I have no skills neither in Laz4Android, nor in LAMW :-[ .
« Last Edit: September 07, 2020, 09:25:57 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: ActivityForResult in Jcamera --Crash
« Reply #2 on: September 07, 2020, 07:56:48 pm »
Hi, Guser979!

Please, update [and test] "jBitmap.java"  from github....

[the error is in file save task ...]


« Last Edit: September 07, 2020, 08:24:58 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Guser979

  • New Member
  • *
  • Posts: 21
Re: ActivityForResult in Jcamera --Crash
« Reply #3 on: September 07, 2020, 09:26:31 pm »
Hi, I tried... after  tests and rebuilds ... same error. on the same line in controls.java

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: ActivityForResult in Jcamera --Crash
« Reply #4 on: September 07, 2020, 10:14:04 pm »

Ok.

Can you test "AppCameraDemo" 

[after update jBitmap.java  in LAMW folder "......\android_wizard\smartdesigner\java"  ]
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Guser979

  • New Member
  • *
  • Posts: 21
Re: ActivityForResult in Jcamera --Crash
« Reply #5 on: September 07, 2020, 10:29:19 pm »
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: //" ....
« Last Edit: September 07, 2020, 10:34:22 pm by Guser979 »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: ActivityForResult in Jcamera --Crash
« Reply #6 on: September 07, 2020, 10:34:45 pm »

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?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Guser979

  • New Member
  • *
  • Posts: 21
Re: ActivityForResult in Jcamera --Crash
« Reply #7 on: September 07, 2020, 10:37:55 pm »
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.app.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.support.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.support.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.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
09-07 16:51:37.503 10475 13671 13671 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
« Last Edit: September 07, 2020, 11:53:38 pm by Guser979 »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: ActivityForResult in Jcamera --Crash
« Reply #8 on: September 07, 2020, 11:43:03 pm »

Quote
Yes. As soon as I can make it work.

Well, we need ADB Logcat to find/trace the error!

"Tools"  -->  "[LAMW]..." ---> "ADB Logcat" ---> "....[dump]"
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Guser979

  • New Member
  • *
  • Posts: 21
Re: ActivityForResult in Jcamera --Crash
« Reply #9 on: September 07, 2020, 11:55:56 pm »
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.
« Last Edit: September 08, 2020, 01:07:10 am by Guser979 »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: ActivityForResult in Jcamera --Crash[SOLVED]
« Reply #10 on: September 08, 2020, 03:06:36 am »
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?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Guser979

  • New Member
  • *
  • Posts: 21
Re: ActivityForResult in Jcamera --Crash[SOLVED]
« Reply #11 on: September 08, 2020, 04:00:49 am »
It didn't work, JM. Only success without Add Support ...

All my projects, I build in Gradle. But I had never used Add Support ...
« Last Edit: September 08, 2020, 04:02:27 am by Guser979 »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: ActivityForResult in Jcamera --Crash[SOLVED]
« Reply #12 on: September 08, 2020, 04:35:43 am »
Quote
But 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   ]
« Last Edit: September 08, 2020, 04:44:41 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Guser979

  • New Member
  • *
  • Posts: 21
Re: ActivityForResult in Jcamera --Crash[SOLVED]
« Reply #13 on: September 08, 2020, 04:50:32 am »
 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.
« Last Edit: September 08, 2020, 05:36:41 am by Guser979 »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1623
Re: ActivityForResult in Jcamera --Crash[SOLVED]
« Reply #14 on: September 08, 2020, 06:02:00 am »

Quote
Even 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...."]

Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018