I'm trying to make an Android Wear project with Offline Voice Recognition. It works online, connected with the phone, but i want the Wear Device to be independent.
This is my code working for online recognition :
private static final int SPEECH_RECOGNIZER_REQUEST_CODE = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
startSpeechRecognition();
}
private void startSpeechRecognition() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, SPEECH_RECOGNIZER_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_RECOGNIZER_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String recognizedText = results.get(0);
Log.d("VOICE TEST : ", recognizedText);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Thanks a lot.
The Android Wear speech recognizer requires a connection to the phone and the internet to work. So what you are trying to achieve is not possible.
You can test this out yourself by disconnecting the phone from the wearable, and then trying to use any of the "Ok Google" commands. It does not work.
Related
I'm trying to automatically add Turkish subtitles under any selected video in android until the video ends, and I've come to the conclusion that the best option for this is google cloud speech to text api. But I couldn't find enough resources on the internet or they didn't explain very well. Can someone who knows this tell me how I can integrate it in detail? It would be great if you explain with java code examples.
video.java
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == RESULT_OK) {
if (data != null) {
binding.videoview.setVideoURI(data.getData());
MediaController mediaController = new MediaController(this);
binding.videoview.setMediaController(mediaController);
mediaController.setAnchorView(binding.videoview);
binding.videoview.requestFocus();
binding.videoview.start();
//I want it to automatically add subtitles under the video as soon as the
//video starts.
System.out.println("data.getdatastring: "+data.getDataString());
if (!mIsListening)
{
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
}
}
}
private void callChooseFromVideo()
{
binding.btnChooseVideo.setVisibility(View.INVISIBLE);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 2);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityVideoConferenceBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
callChooseFromVideo(); // <-- function to select any video in emulator
}
I'm currently learning how to use android studio in java and am trying to make a social media app. I am currently creating an edit profile page where the user would update their details and upload a profile picture.
I have been following tutorials online and all of the ones I have come across use the startActivityForResult method. It has been crossed out and wont call the method as it is deprecated. But I don't know what to use instead.
`ProfileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//open gallery
Intent OpenGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(OpenGalleryIntent, 1000);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #androidx.annotation.Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000){
if(resultCode == Activity.RESULT_OK){
Uri imageUri = data.getData();
ProfileImage.setImageURI(imageUri);
UploadImageToFirebase(imageUri);
}
}
}
private void UploadImageToFirebase(Uri image){
StorageReference fileRef = storageReference.child("Profile.jpg");
fileRef.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Edit_Profile.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
});![enter image description here](https://i.stack.imgur.com/padRc.jpg)`
I know there is an alternative but I don't understand how it works.
startActivityForResult is indeed deprecated in later versions of AndroidX Activity and Fragment APIs (while I believe you can still use it despite of warning). New way to get result from activity is registerForActivityResult.
In your code you would need to create a launcher, which will handle result (selected image)
private final ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK
&& result.getData() != null) {
Uri photoUri = result.getData().getData();
//use photoUri here
}
}
);
and then launch this launcher in onClickListener
profileImage.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
launcher.launch(intent);
});
Yes startActivityForeResult is deprecated.
Now you can use ActivityResultLauncher for the callbacks
https://developer.android.com/training/basics/intents/result#java
I need to build an app which will recognize QR codes without forcing the user to install other apps. In future, I will also need to manipulate the scanned image before recognizing the code (codes scanned by me will have inverted colors).
Trying to follow hints and tutorials from these links:
Integrating the ZXing library directly into my Android application
Embedding ZXing in android app
http://karanbalkar.com/2013/12/tutorial-65-implement-barcode-scanner-using-zxing-in-android/
After creating some basic code and running the app I click my Scan button and get an error that No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }
What I've done:
Create new project
Copy core-3.2.1.jar to libs/
Add intent calling and result handling
intent/result code added by me:
private Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan= (Button)findViewById(R.id.btnScan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
MultiFormatWriter writer = new MultiFormatWriter();
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.i("App","Scan unsuccessful");
}
}
}
How to start the intent? What am I doing wrong?
you should launch scan like this:
#Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
//in case you want to customize a bit.
integrator.setPrompt("Scan a QR/Bar code");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.initiateScan();
}
Receive results like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String data = scanResult.getContents();
// use this data
} else {
// error
}
break;
}
}
}
Edit 1:
Add this to build.gradle of your app as dependencies:
compile 'com.journeyapps:zxing-android-embedded:3.0.2#aar'
compile 'com.google.zxing:core:3.2.0'
Try the below link it worked for me:
https://github.com/dlazaro66/QRCodeReaderView
I'm making an Android app that is web base social media sites all in one and I am having trouble with Twitter and Google plus won't allow me to upload a photo, it will let me choose one but will not let me upload it.
Facebook is fine that will let me choose a photo and upload it without errors.
Here is the code I'm using, maybe something I'm doing is wrong.
#SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
hello.mUploadMessage = uploadMsg;
getActivity().startActivityForResult( Intent.createChooser( i, "File Chooser" ), 1 ); }
MainActivity
ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
I'm using Eclipse and testing it on Android 4.4.4 and 4.3, can anyone give me an idea? hope it something basic and an easy fix.
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);
}