0

I'm trying to access a variable from onActiviyResult, but I do not get the latest value

I'm using a camera EXTRA_OUTPUT intent - (not trying to get the intent data, trying to locate the path)

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_IMAGE1) {
        if (resultCode == RESULT_OK) {
            Log.e ("TAG","file path = "+imageFilePath1);

imageFilePath1 is declared under the screen main activity:

String imageFilePath1 = "";

After creating the image file, imageFilePath1 has the correct path. what is the problem and what are the solutions?

Jack
  • 5,354
  • 2
  • 29
  • 54
SO 80
  • 197
  • 2
  • 11
  • you did not initialize path value in onActivityResult – Muhammad Muzammil Sharif Apr 02 '19 at 20:55
  • what do you mean? the initializing was under the main activity to "". then the file was created and the path value was populated. – SO 80 Apr 02 '19 at 20:59
  • you need to update its value in onActivityResult method which is somewhere in ` Intent data` parameter – Muhammad Muzammil Sharif Apr 02 '19 at 21:01
  • You didn't put anything that assigns the actual path to `imageFilePath1`. Did you forgot to include it here? – Jack Apr 02 '19 at 21:01
  • jackz314 the actual path is assigned before, in the create file method. – SO 80 Apr 02 '19 at 21:07
  • Muhammad do you have an example? – SO 80 Apr 02 '19 at 21:07
  • i've noticed this solution - https://stackoverflow.com/a/33206708/10763244, is there something better? – SO 80 Apr 02 '19 at 21:09
  • Are you trying to update `imageFilePath1` or simply access it, if you are just accessing it and it's null there's probably something wrong when you first assigned value to it. – Jack Apr 02 '19 at 21:39
  • In declaration imageFilePath1 = "". after I create a file in another method it populated with the path, and then in onActivityResult it's value is "" again. maybe the declaration is wrong? – SO 80 Apr 02 '19 at 21:51
  • See also: https://stackoverflow.com/a/46424861/115145 – CommonsWare Apr 02 '19 at 22:15
  • @CommonsWare - I have to use savedInstanceState to get to the path?, is there a simple solution? the path returns to main activity and gets initialized again – SO 80 Apr 02 '19 at 22:44
  • @SO80: "I have to use savedInstanceState to get to the path?" -- yes. There is no guarantee that your process will remain around while the camera app is in the foreground. "is there a simple solution?" -- that *is* the simple solution. – CommonsWare Apr 02 '19 at 22:49
  • @CommonsWare - how and where can I use savedInstanceState when the main activity is calling a method, that calls a method that creates the path? do you have an example? the flow is - main activity -> camera clicked method -> open camera method - > create file - > intent - > on activity result - > main activity – SO 80 Apr 02 '19 at 23:14
  • @SO80: There is an example in [the second Stack Overflow answer that I linked to](https://stackoverflow.com/a/46424861/115145). [Here is the complete project](https://github.com/commonsguy/cw-omnibus/tree/FINAL/Camera/FileProvider), and [here is the book that goes over that project](https://commonsware.com/Android/) (along with hundreds of other samples). – CommonsWare Apr 02 '19 at 23:17

0 Answers0