Email bitmap image in Android - java

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

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 app unjustifiably rotates to landscape and then back to portrait

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"

Unable to access getPackageManager GetActivityInfo from VideoDetailsFragment in Android Leanback app

I want to make an app for Android TV, so I am using the default Android TV activity in Android Studio as a template for my project, I want to add a button in the VideoDetailsFragment, this button only appears after the package manager detects that a secondary activity is active.
If the package manager detects my secondary activity is active and running it should show a button to disable it and if the activity is disabled it should show a button to enable it, but... the package manager can't get the name of the activity, it can't actually get the name of any package or activity, it always show NameNotFoundException both of my activities are android:name="android.intent.action.MAIN" in the Manifest because I want to add a launch intent for both from the launcher, but the secondary activity has the label android:enabled="false" because the button I want to put in the VideoDetailsFragment is supposed to enable and disable that activity.
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.software.leanback"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.Leanback">
<activity
android:name=".MainActivity"
android:banner="#mipmap/banner"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:logo="#mipmap/banner"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailsActivity" />
<activity android:name=".PlaybackOverlayActivity" />
<activity android:name=".BrowseErrorActivity" />
<activity android:name=".SecondaryActivity"
android:icon="#mipmap/ic_launcher"
android:banner="#mipmap/banner"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
</application>
Here is the VideoDetailsFragment code:
private void setupDetailsOverviewRow() {
Log.d(TAG, "doInBackground: " + mSelectedMovie.toString());
final DetailsOverviewRow row = new DetailsOverviewRow(mSelectedMovie);
row.setImageDrawable(getResources().getDrawable(R.drawable.default_background));
int width = Utils.convertDpToPixel(getActivity()
.getApplicationContext(), DETAIL_THUMB_WIDTH);
int height = Utils.convertDpToPixel(getActivity()
.getApplicationContext(), DETAIL_THUMB_HEIGHT);
Glide.with(getActivity())
.load(mSelectedMovie.getCardImageUrl())
.centerCrop()
.error(R.drawable.default_background)
.into(new SimpleTarget<GlideDrawable>(width, height) {
#Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable>
glideAnimation) {
Log.d(TAG, "details overview card image url ready: " + resource);
row.setImageDrawable(resource);
mAdapter.notifyArrayItemRangeChanged(0, mAdapter.size());
}
});
/******************************/
/*HERE IS THE PROBLEMATIC CODE*/
/******************************/
PackageManager pm = getActivity().getPackageManager();
ComponentName cn = new ComponentName(getActivity(), "com.valecast.myapp.SecondaryActivity");
ActivityInfo info = pm.getActivityInfo(cn, 0); //<===IT NEVER LOCATE THIS
if (info != null && info.enabled) {
// Component is enabled
row.addAction(new Action(ACTION_BUY, "it works"));
} else {
// Component is disabled
row.addAction(new Action(ACTION_BUY, "it doesn't"));
}
/******************************/
row.addAction(new Action(ACTION_WATCH_TRAILER, getResources().getString(
R.string.watch_trailer_1), getResources().getString(R.string.watch_trailer_2)));
row.addAction(new Action(ACTION_RENT, getResources().getString(R.string.rent_1),
getResources().getString(R.string.rent_2)));
row.addAction(new Action(ACTION_BUY, getResources().getString(R.string.buy_1),
getResources().getString(R.string.buy_2)));
mAdapter.add(row);
}
And here is a screenshot of the error show in android studio:
Here is another screenshot showing one of the suggested answer, but it doesn't work neither:
I wrapped that into a try catch, but when I run my app it always throw the NameNotFoundException in the Android Monitor.
What I am doing wrong?
You need to define the proper ComponentName:
new ComponentName("com.valecast.myapp", "com.valecast.myapp.SecondaryActivity");
and have a try catch:
try {
ActivityInfo info = pm.getActivityInfo(cn, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

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>

How to get Spinner OnListItemSelected to work

I am currently trying to implement a Spinner within my android application. I am having trouble with getting the OnItemSelected method, to open a new class based on what item was selected.
I have the code shown bellow, which does not seem to work, Also since adding this is, it now from the menu when I click the button to open Film and TV it opens the wrong layout, but nothing changed other than adding the bellow code.
What should happen: Activity Starts --> Click on Film and TV --> Select item from Spinner --> New class opens based on what Item was selected.
What Happens now: Activity Starts --> Click on Film and TV --> Wrong layout opens --> Press back on phone --> Right layout opens --> Select item from Spinner --> Nothing Happens
Code:
String classes[] = {"SeanConnery", "BillyConnoly", "JamesMcAvoy", "KarenGillan", "KellyMacdonald", "AshleyJensen"};
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String classSpot = classes[pos];
try{
Class nextClass = Class.forName("com.example.famouspeople." + classSpot);
Intent ourIntent = new Intent(Film.this, nextClass);
startActivity(ourIntent);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.famouspeople"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.famouspeople.MainMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.famouspeople.Film"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.SeanConnery"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.BillyConnoly"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.JamesMcAvoy"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.KarenGillan"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.AshleyJensen"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.KellyMacdonald"
android:label="#string/app_name" >
</activity>
</application>
to create intent you can do something like this...
final Context context = this;
Intent intent = new Intent(context,youractivity.class);
startActivity(intent);
change your mainfest from
<activity
android:name="com.example.famouspeople.SeanConnery"
android:label="#string/app_name" >
</activity>
To
<activity
android:name=".YourJavaClassName"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.famouspeople.SeanConnery" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Categories