I have tried to install OpenCV for Java-App Development in Android Studio. The app starts on the emulator and tried to load but instantly shuts down. This happens when the app encounter anything related to OpenCV in the code (excluding imports).
This is not the entire project, if you want to compile and need everything drop a comment
package com.example.cse535a1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.content.Context;
import android.view.View;
import android.hardware.*;
import android.widget.FrameLayout;
import org.opencv.core.*;
public class MainActivity extends AppCompatActivity {
Camera c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))) {
this.finish();
System.exit(0);
}
// System.loadLibrary(Core.NATIVE_LIBRARY_NAME); ---------------------------------CRASHES HERE
// Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
// Log.i("OPENCV", mat.dump());
Button button_symptoms = (Button)findViewById(R.id.button_symptoms);
Button button_upload_signs = (Button)findViewById(R.id.button_upload_signs);
Button button_measure_heart_rate = (Button)findViewById(R.id.button_measure_heart_rate);
Button button_measure_respiratory_rate = (Button)findViewById(R.id.button_measure_respiratory_rate);
c = getcam();
CameraView cv1 = new CameraView(getApplicationContext(), c);
FrameLayout view_camera = (FrameLayout)findViewById(R.id.view_camera);
view_camera.addView(cv1);
button_symptoms.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
Intent intent = new Intent(getApplicationContext(), Loggin_symptoms.class);
startActivity(intent);
}
});
button_upload_signs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
}
});
button_measure_heart_rate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
}
});
button_measure_respiratory_rate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
SensorManager manager_sensor = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor_accelerometer = manager_sensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
});
}
I have previously done some research before posting on StackOverflow and I found this->
Android crashes when using OpenCV from activity
This has led me to the install instructions on-> Installing OpenCV 2.4.13.7. The necessary part can be found under Application Development with Static Initialization.
But unfortunately, the entire process of having an Android.mk file is no longer in use. So to solve this problem what do I do now?
Is there a different file where the OPENCV_INSTALL_MODULES:=on line must be inserted?
So, I think I have found the solution to this problem in my case.
Upon looking in the Logcat tab in Android studio, I noticed a problem that libopencv_java451.so was not found. In the jniLibs folder there was instead libopencv_java4.so.
A simple rename fixed this problem. There are two places where I renamed the files.
jniLibs folder in the project directory
jniLibs in the opencv sdk directory (though this one might be ultimately responsible)
Depending on the system (for me-> x86 and x86_64), rename your files.
I have attached a pic showing my project file structure, look at the large yellow marks which show the renamed files.
Related
I have the following code , im receiving an error :
enter image description here
package com.example.photopicker;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button addimage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addimage=findViewById(R.id.button_pick_photo);
addimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Registers a photo picker activity launcher in single-select mode.
ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
// Callback is invoked after the user selects a media item or closes the
// photo picker.
if (uri != null) {
Log.d("PhotoPicker", "Selected URI: " + uri);
} else {
Log.d("PhotoPicker", "No media selected");
}
});
// Include only one of the following calls to launch(), depending on the types
// of media that you want to allow the user to choose from.
// Launch the photo picker and allow the user to choose images and videos.
pickMedia.launch(new PickVisualMediaRequest.Builder()
**.setMediaType(new ActivityResultContracts.PickVisualMedia.ImageAndVideo())**
.build());
}
});
}
}
This code i got it from the Android developer Website :
https://developer.android.com/training/data-storage/shared/photopicker
but Doesnt seem to work , and im not able to find any online solution.
Try replacing:
new ActivityResultContracts.PickVisualMedia.ImageAndVideo()
with:
ActivityResultContracts.PickVisualMedia.Companion.getImageAndVideo()
ImageAndVideo is a Kotlin object — it is not a class that you instantiate yourself. However, the source code lacks the #JvmField annotation, so I think that just referring to ActivityResultContracts.PickVisualMedia.ImageAndVideo will fail, as outlined in the docs.
I'm working on a simple number guessing game. At the moment I have it set that when you guess the correct number it takes you to WinActivity.java. All I want it to do on that screen is say "You won" and to play a little trumpet victory sound (and also display a button that'll bring them to the GameActivity if they want to play again).
The trouble is, when I try bringing in a MediaPlayer the app doesn't even start.
Here is my code for my WinActivity that contains the MediaPlayer.
package com.example.jeremy.numberguessinggame;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class WinActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_win);
MediaPlayer trumpetsound = MediaPlayer.create(this,R.raw.trumpet);
trumpetsound.start();
Button btnPlayAgain = (Button) findViewById(R.id.button2);
TextView youWon = (TextView) findViewById(R.id.textViewYouWon);
btnPlayAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent playAgainIntent = new Intent(WinActivity.this,GameActivity.class);
startActivity(playAgainIntent);
}
});
}
}
When I run the app the Messages Gradle Build window will pop up and says "error: cannot find symbol variable raw".
I added the raw folder to the res location, and was able to drag the sounds into the folder no problem.
I just don't see why I can't make this MediaPlayer work.
I would love some help. If you need more information I'll be glad to give it to you.
Hi I have been racking my brain for hours, did research online but nobody seems to have an answer. My emulator was running my code no problem then I ran it again and I get "Session 'MainActivity': error". I looked through this main activity about 10 times but there is no error sign anywhere and it looks like it should be working fine, I mean it was working before no problem. So I'm not sure if there really is a problem I don't see that Android Studio is not pointing out properly or if this is a different problem all together.
Any help would be greatly appreciated. Thank you.
package tekvision.codedecrypter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import gameInfo.GameDatabase;
public class MainActivity extends ActionBarActivity {
//Runs before the application is created
private Button mCampaignButton;
private final Context context = this;
//When the application is created
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiate a GameDatabase object (this activity)
final GameDatabase gDB = new GameDatabase(context);
gDB.fillGameDatabase();
//Keeps screen on so it doesn't fall asleep
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Finding button by button id after application is created
mCampaignButton = (Button)findViewById(R.id.campaignButtonID);
//Checks if the campaign button is clicked
mCampaignButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ans = gDB.getAnswer("Ancient",1);
//Toast pop up message
Toast toast = Toast.makeText(getApplicationContext(),
ans ,
Toast.LENGTH_SHORT);
toast.show();
//Intent to go from main activity to campaign Level Select Activity
final Intent intent = new Intent(MainActivity.this, CampaignSelectLevel.class);
startActivity(intent);
}
});
}
}
Try rebuilding and cleaning your project.
Build > Rebuild Project
and
Build > Clean Project
My app crashes straight away with an exception
Here is the code of my main activity
package com.example.orientation;
import android.os.Bundle;
import android.app.FragmentManager;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity implements myList.Communication {
myList listFragment;
Details detailsFragment;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=getFragmentManager();
listFragment = (myList) manager.findFragmentById(R.id.listFragment);
listFragment.setCommunicator(this);
}
#Override
public void respond(int index) {
detailsFragment = (Details) manager.findFragmentById(R.id.detailFragment);
if (detailsFragment!=null && detailsFragment.isVisible()) {
detailsFragment.changeData(index);
}else{
Intent intent = new Intent(this,DetailActity.class);
intent.putExtra("index",index);
startActivity(intent);
}
}
}
I tried reading documentation but could not get how to solve this. Any help will be appreciated. Thank You.
If you have only added android support jar, you must add the whole Appcompat project to your workspace:
right click -> IMport -> Android -> Project from Existing Source
Then go to SDK Folder where you installed and find extras/android/support/v7/appcompat
and add this whole Library.
It seems like you are missing resources needed for ActionBarActivity
hey i am new to android development. i tried exporting my app the process failed because of some minor errors and then i got errors all over my code. it was working perfectly fine with no errors before attempting to export.
check these two pics of the xml files where the errors popped up
main activity file: http://postimg.org/image/4gwdipnap/
second activity file: http://postimg.org/image/vuy1ryba3/
I didn't have any of these errors before trying to export the project.
heres the code for both of the files
main activity page
package youngadults.camden;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
public class FrontPage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
findViewById(R.id.uyounadults).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(FrontPage.this, youngadultsp.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
}
});
}
}
second activity page
package youngadults.camden;
import android.app.Activity;
import android.os.Bundle;
public class youngadultsp extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.youngadultsp_1);
}
}
You need to set Android API level, do you use Android Studio or Eclipse?
For Eclipse edit your Manifest file (19 is example):
<uses-sdk android:minSdkVersion="19" />
For Android Studio
Right click the App directory
Select "Open Module Setting" at the bottom
Under Modules select app
Set 'Compile Sdk version' as you want