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);
}
Related
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.
I have to throw in the towel on this. I am trying to use Image Cropper: Arthur Hub in a Fragment and I keep getting this
error: onActivityResult(int,int,Intent) in ProfileFragment cannot
override onActivityResult(int,int,Intent) in Fragment attempting to
assign weaker access privileges; was public
Here is the imageCropper function in the fragment:
private void ImagePicker() {
CropImage.activity(mainImageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(startActivityForResult();,this);
}
And here is the onActivityResult in the same fragment I am using to obtain the image:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == Activity.RESULT_OK) {
mainImageUri = result.getUri();
profileImage.setImageURI(mainImageUri);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
I had this implemented previously in an activity and it worked fine. As soon as I adjusted it to work in a Fragment, I cannot proceed.
Please help! Also I am a relatively new developer so please be a bit more descriptive in your explanation. Thanks!
In fragment overrides onActivityResult like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { //note: public, not protected.
super.onActivityResult(requestCode, resultCode, data);
}
and in fragment call startActivityForResult method.
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.
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();
}
}
}
So whenever I select a picture from the gallery in my app, it crashes. Here is the code for the button to the gallery and selected picture to the imageview.
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() { //opens the gallery
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView2.setImageURI(imageUri);
}
}
Please comment if you need more information, I desperately need help, as this is a very major roadblock for me.
Without the Logs, I can recommend on the following:
Make sure that pickImageButton isn't null.
Make sure that imageView2 isn't null.
Make sure that in manifest your Activity is in:
android:launchMode="singleTop"