Android app unjustifiably rotates to landscape and then back to portrait - java

Android app unjustifiably rotates to landscape for a split second and then returns to portrait. This happens when returning to the app from the camera.
public void configureItemImageButton() {
MaterialCardView itemImageButton = (MaterialCardView) view.findViewById(R.id.itemImageCardView);
itemImageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 111);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 111) {
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) view.findViewById(R.id.itemImage);
imageview.setImageBitmap(image);
}
}
Below is my android manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/ic_trainsterlogo"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:screenOrientation="portrait">
<activity android:name=".TrainsterActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustNothing"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CAMERA"> </uses-permission>
EDIT: This problem did not occur if my editTextview had text in it, however I can no longer reproduce this, and I tried removing the textview and the problem persists.
EDIT: I have negated it's negative affect of clearing my image by saving the instance. However the unexplained rotation continues to be aesthetically unpleasant.

Add this in your manifest file under that particular activity
android:screenOrientation="portrait"

Related

Problem when I try to open another window from a button the app closes completely "Android Studio"

i'm a student and i'm working on a mobile app.
The probleme is that my app was working, but now when i click on the button to open another window it closes.
Here is my code:
`
public class MainActivity extends AppCompatActivity {
Button b_inscrire_etudiant;
Button b_inscrire_enseignant;
Button b_inscrire_admin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_inscrire_etudiant = findViewById(R.id.b_inscrire_etudiant);
b_inscrire_enseignant = findViewById(R.id.b_inscrire_enseignat);
b_inscrire_admin = findViewById(R.id.b_inscrire_admin);
b_inscrire_etudiant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this , InscriptionEtudiant.class);
startActivity(i);
}
});
b_inscrire_enseignant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this , InscriptionEnseignant.class);
startActivity(i);
}
});
b_inscrire_admin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this , InscriptionAdmin.class);
startActivity(i);
}
});
}
}
`
I worked as usual "using Intent" besides it worked well until now.
Here is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myusto">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MyUsto"
tools:targetApi="31">
<activity
android:name=".InscriptionEnseignant"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".InscriptionEtudiant"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".InscriptionAdmin"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".Connexion"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
ps: I solved the problem, by the way two were missing
Thank you for the answers.
It's true, it was missing a part of the code which was not written in the manifest file

Android file picker from external storage not working on API level > 24

I have running code of choose file from file manager , its working fine on API level 23 but after that it is giving problem on latest devices. Now by google i knew that to use it on latest device i need to use content provider , i applied some code but it's not working . I want only path of selected file and file will be audio.
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.contentproviderfilechoose"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
</application>
Java code:
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Uri uri = null;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(uri, "*/*");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} else {
requestStoragePermission();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri selectedFileUri = data.getData();
//selectedFilePath = FilePath.getPath(MainActivity.this, selectedFileUri);
} else { // Result was a failure
Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
}
}
}
Please add READ_EXTERNAL_STORAGE permission in AndroidManifist.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
here you may get error
android:authorities="your_package_name.contentproviderfilechoose"
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.contentproviderfilechoose"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>

Email bitmap image in Android

I am trying to make it so upon clicking a button, my app takes a screenshot of the current screen programmatically and stores it in a variable (this I have managed to do). I would then want it to send an email, attaching that bitmap image to it.
(I have tried hard coding the email address etc in)
When I have tried to send an email with simple text for the body it has worked fine, but when I try to attach an image it doesn't work.
Button emailBtn = (Button) findViewById(R.id.emailBtn);
main = findViewById(R.id.main);//email form button
emailBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap b = screenshot.takescreenshotOfRootView(main);
//ImageView programLogo = (ImageView) findViewById(R.id.programLogo);
//programLogo.setImageBitmap(b);
String subject = "please work";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","myemail#gmail.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, b);
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
}
});
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.computingproject">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Computing Project"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Main11Activity"></activity>
<activity android:name=".Main10Activity" />
<activity android:name=".Main9Activity" />
<activity android:name=".Main8Activity" />
<activity android:name=".Main7Activity" />
<activity android:name=".Main6Activity" />
<activity android:name=".Main5Activity" />
<activity android:name=".Main4Activity" />
<activity android:name=".Main3Activity" />
<activity android:name=".Main2Activity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Screenshot.java
import android.graphics.Bitmap;
import android.view.View;
public class screenshot {
public static Bitmap takescreenshot(View v) {
v.setDrawingCacheEnabled(true);
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return b;
}
public static Bitmap takescreenshotOfRootView(View v) {
return takescreenshot(v.getRootView());
}
}
Any help will be appreciated! Thank you

when i want to test my app with a real device the app doesnt start

I've been trying to test my app where I'm implementing a G+ sign in and when I run it in the emulator with Android Studio the app runs but says that I need to update Google Play Services.
I've been struggling to do that so I thought that I would just test it on a real device but when I do the app doesn't start/run. When I go to settings and apps on device I can see that the app is installed but it doesn't show up in the app drawer or anywhere else.
I get this exception under my run tab in the bottom left corner:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.madchallenge2016edwindaniel.upbirdwatchers/.LoginActivity } from null (pid=20620, uid=2000) not exported from uid 10154
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1948)
at com.android.commands.am.Am.runStart(Am.java:463)
at com.android.commands.am.Am.run(Am.java:108)
at com.android.commands.am.Am.main(Am.java:81)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:276)
at dalvik.system.NativeStart.main(Native Method)
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar" />
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<activity android:name=".RegisterActivity" />
<activity android:name=".LoginActivity" />
</application>
Here is the code of my launch activity:
package com.madchallenge2016edwindaniel.upbirdwatchers;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import android.view.View;
import com.google.android.gms.common.ConnectionResult;
public class LoginActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
private SignInButton mSignInButton;
private static final int RC_SIGN_IN = 9001;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Background
getWindow().setBackgroundDrawableResource(R.drawable.login_background);
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
SignInButton mSignInButton = (SignInButton)findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
// ...
}
}
});
}
//Start sign in
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
} else {
// Signed out, show unauthenticated UI.
// updateUI(false);
}
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void setmSignInButton(SignInButton mSignInButton) {
this.mSignInButton = mSignInButton;
}
}
After editing my manifest file according to the answer I got, my app now runs.
This is what my updated manifest file looks like:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".LoginActivity"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".RegisterActivity" />
<activity android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar"/>
</application>
You missed Intent filter in android manifest
Please replace your manifest with below code...
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".RegisterActivity" />
<activity android:name=".LoginActivity" />
</application>
Intent filters show when to launch this activity. usually you will have one activity with intent filter that is to show that it is first activity when application is launched.

Apk android ActivityNotFoundException

I want to run an implicitly tried with the following code:
MenuActivity.java
public class MenuActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button btn = (Button) findViewById(R.id.btnVideo);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri intentUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pp);
intent.setDataAndType(intentUri,"audio/mp3");
startActivity(intent);
}
});
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/iconopp14"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.powerpump.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MenuActivity" />
</application>
You need to specify more parameters in AndroidManifest.xml?
Thanks.
I guess you simply have to register your MenuActivity in you AndroidManifest.xml as demonstrated here.
Looking at your code, it looks like you created a MenuActivity class after creating an android project with default settings and MenuActivity is your main activity(Launcher) class.
Just replace the MainActivity with MenuActivity in your AndroidManifest.xml file and delete
<activity android:name=".MenuActivity" />
OR
Replace the class name to MainActivity from MenuActivity in your java code as follows
public class MainActivity extends Activity {
......
OR
use this as your android manifest.xml file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/iconopp14"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.powerpump.MenuActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Categories