Sending a URI from one method to another one - java

A URI is got through this known code in the following method as data.getData():
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 8010:
Log.i("Test", "Result URI " + data.getData());
break;
}
}
Now, there is another method (e.g. public void dosomething() {}) in the whole code where the URI is needed.
How can one get the URI, i.e. data.getData(), in dosomething()?
The use of data.getData() was tried in dosomething() without success. What works easily with standard variables normally, seems not to work with Uris.
ADDENDUM
Here a concrete example of code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dosomething() {
[--- here I need to get my data.getData() from onActivityResult() (directory chosen by user in startChoose()) --- something like: mypath = data.getData(); ---]
File imagedir = getExternalStoragePublicDirectory(mypath);
[...]
}
public void startChoose(View view) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, "Choose directory"), 8010);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 8010:
Log.i("Test", "Result URI " + data.getData());
break;
}
}
}
User action:
A user choose a directory by clicking a button, which leads to startChoose() with onClick="startChoose" in the XML. onActivityResult() gives access to the chosen directory (=data.getData()) and this path shall be used in dosomething() but I don't succeed at getting the chosen directory (=data.getData()) in dosomething().
PS: getExternalStoragePublicDirectory() is deprecated but so far it can be used here (or you can update it as per API 29 if you want).
Basically, what I need is that the user chooses his/her directory by clicking on a button and based of that directory, the code continue its execution in dosomething().
Thanks in advance.

ok, after searching for a while, I found the following to transmit the Uri via methods:
The class must begin with this variable declaration:
public class MainActivity extends AppCompatActivity {
public Uri mypath;
onActivityResult can be like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 8010:
mypath=data.getData();
break;
}
}
And then, you can use everywhere in the code the variable mypath as following:
File mydirectory = getExternalStoragePublicDirectory(mypath.toString());
But mypath is the complete path of the directory and needs to be reduced to the environment path in order to work here. That's another game.

Related

LinkedIn Authentication not working on Fragment in android

I am creating an app that has the LinkedIn login. I am following this documentation. But when I click on the login button, the app redirected to the LinkedIn app and asking for login. When successful login it redirected to my app. But nothing happens. Nothing happens on onActivityResult also.
Below is my code .Login implemented on fragment
LISessionManager.getInstance(getActivity()).init(getActivity(), buildScope(), new AuthListener() {
#Override
public void onAuthSuccess() {
getLinkedInProfile();
Toast.makeText(getApplicationContext(), "success" , Toast.LENGTH_LONG).show();
}
#Override
public void onAuthError(LIAuthError error) {
Toast.makeText(getApplicationContext(), "failed " + error.toString(), Toast.LENGTH_LONG).show();
}
}, true);
//
private static Scope buildScope() {
return Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS);
}
and onActivityResult as follows:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LISessionManager.getInstance(getActivity()).onActivityResult(getActivity(), requestCode, resultCode, data);
}
Already added hash and package name on LinkedIn developer console. Did I am missing anything? Please help
It is found that the onActivityResult for LinkedIn sdk triggres on the parent activity rather than fragment onActivityResult. So you have to write following code into your parent Activity's onActivityResult for triggering fragment's onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
yourFragment.onActivityResult(requestCode, resultCode, data);
}
Try Log to ensure whether function is called.

Can't Pass Parcelable via Intent

I have two activities:
ListViewActivity
AddActivity
and one object class :
todoObj
I have a list view in my ListViewActivity activity and when I click add button this will initiate AddActivity . In AddActivity when user enters Title, choose date, time and the category I want AddActivity to create a todo object and pass this back to the ListViewActivity.
Sorry I could not share the code itself, it always gives this error all the time so I uploaded on github please check it out.
Sorry again.
Many thanks.
All you have to fix onActivityResult method in ListViewActivity:
Because of you setting result code ADD_REQUEST_CODE in AddActivity "setResult(ListViewActivity.ADD_REQUEST_CODE, intent);", you should use "if (resultCode == ADD_REQUEST_CODE) {" in ListViewActivity not RESULT_OK.
You should receive intent from onActivityResult not ListViewActivity's intent. getIntent() gives ListViewActivity's intent. So use data variable:
onActivityResult(int requestCode, int resultCode, Intent data)
Final code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_REQUEST_CODE) {
if (resultCode == ADD_REQUEST_CODE) {
Log.i("ListViewActivity", "Returned onActivityResult");
TodoObj todoObj = (TodoObj) data.getParcelableExtra("EXTRA_TODO");
Toast.makeText(ListViewActivity.this, "" + todoObj.getmYear(),
Toast.LENGTH_SHORT).show();
}
}
}

startActivityForResult out an activity

How to use startActivityForResult outside an activity context say in a regular java class
EX:
In the main activity, Im calling method of SampleClass. In this method i have to open the device camera and obtain the image and process it.
The problem is the control is not going to the onActivityResult callback method inside the SampleClass but instead it is going to onActivityResult callback method inside the MainActivity. What am i doing wrong?
I need to be able to handle the image inside Sample Class only. How can i achieve this
Code Snippet
MAIN ACTIVITY:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
...
SampleClass sample = new Sample(this);
sample.openCamera();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == RESULT_OK){
syso("INSIDE MAIN ACTIVITY");
}
if(resultCode == RESULT_CANCELED){
...
}
}
}
}
SAMPLE CLASS:
public class SampleClass extends Activity{
private Context context;
public SampleClass(Context context){
this.context = context;
}
public void openCamera(){
Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity) context).startActivityForResult(photoIntent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == RESULT_OK){
//get the image
// process it.
syso("INSIDE SAMPLE CLASS");
}
if(resultCode == RESULT_CANCELED){
}
}
}
}
If i run this program, The output on the console is INSIDE MAIN ACTIVITY. What i want is to be able to get INSIDE SAMPLE CLASS as the output.
Thanks and Regards
Instead of using
SampleClass sample = new Sample(this);
sample.openCamera();
just use
Intent intent = new Intent(this, SampleClass .class);
and write SampleClass onCreate call openCamera().
Hope this will help.
the problem is that you open the camera using MainActivity Context!!
sure will triggered onActivityResult() of MainACtivity.
((Activity) context).startActivityForResult(photoIntent, 0);
see context object = MainActivity check you SimpleClass constructor.
public SampleClass(Context context){
// when you call from mainActivity and send **this** it means context = MainActivty
this.context = context;
}
if you want SampleClass onActivityResult() triggered there is no way else extends Activity
then call it like normall way ....
Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
context.startActivityForResult(photoIntent, 0); // context must = SampleClass (); no other way
but if you still want to handel image in SimpleClass then follow this code remove extends Activity
in SimpleClass
public static void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0){
if(resultCode == RESULT_OK){
//get the image
// process it.
syso("INSIDE SAMPLE CLASS");
}
if(resultCode == RESULT_CANCELED){
}
}
}
and in you MainActivity class
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SimpleClass.onActivityResult(requestCode,resultCode,data);
}
never ever create new object from activity using new key word Adnroid OS will do that for you when you say
Itnent foo = new(context,foo.class);
and if you did maybe this means hack Code and android OS.
i hope you understand and hope this helps
It is impossible. onActivityResuly will be called on that activity, which have ran another activity.
So if you took activity1 instance to run another activity, another activity will call activity1.onActivityResult()

Calling onActivityResult to determine when a file has been received

I have a WebActivity that sets a JavascriptInterface for a WebView, on the Interface I have created various methods to be called with Javascript for different tasks, one of them is to send a file through Bluetooth.
The file sends just fine, but I want to inform the user when the file has already been accepted and transmited completely on the other end.
I am trying to use onActivityResult on my main WebActivity and also have tried on the separate class that handles the Bluetooth transmission and has a method called on the JavascriptInterface but I am not getting any callback results after the file is succesfully sent:
//First attempt
public class WebActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//some code here
//Here I set the interface
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == 0) {
//This message is shown after the file transfer has started but no
//finished
Toast.makeText(this, "File Sent", Toast.LENGTH_SHORT).show();
if(resultCode == Activity.RESULT_OK){
//It doesn't work here either I always get Activity.RESULT_CANCELED (0)
//flag afer the file transfer has started
}
}
}
}
//Second Attempt
public class BluetoothTasks {
//code here
private void startTransferIntent() {
//some more code here
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
File file_to_transfer = new File(Environment.getExternalStorageDirectory() +
"/txtTransfer.txt");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file_to_transfer));
//some more code here
Activity act = (Activity) mContext;
act.startActivityForResult(intent, TRANSFER_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == TRANSFER_RESULT) {
//Not working here either
//code that is not executed
}
}
}
Could you please tell me if there is a way to do this and if so how would be accomplished?. Thank you.

How to record video using Intents?

I need to unserstand how I can record video programatically. Now I use this construction:
public class AndroidLearningActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(captureVideoIntent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
}
}
When the application is opened then the camera will be opened too. I have record some video, but if I press "back" button on the device then the application crushes. Please, explain me, how can I do it? Thank you.
looks as you are pressing back key and data (intent) not get set.so data may be null here
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
}
}
You have problem in this statement
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
When you will press back button recording will be cancelled and you will get data.getData as null since no recording is done.So change your code to following.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
Log.e("result", "result:" + resultCode);
}
super.onActivityResult(requestCode, resultCode, data);
}

Categories