What's wrong with using int variable ? Making the application to crash - android

This is my MainActivity.java
I used // to mark as not using on some lines and found that the line start with int t =
Is the problem that make the application to crash and force closing.
package com.example.camera_test;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 1337;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
#SuppressLint("NewApi") #Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, CAMERA_PIC_REQUEST );
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#SuppressLint("NewApi") protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
int t = thumbnail.getByteCount();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(Integer.toString(t));
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
}
I added a button when I click on it it will open the camera on my device after I take a photo it will show the photo I took on a small window. It's working.
Then I added this 3 lines:
int t = thumbnail.getByteCount();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(Integer.toString(t));
I wanted to show also the ByteCount of the image on screen in my device.
Once I add the line: int t = thumbnail.getByteCount(); there was an error so I did automatic fix and it added #SuppressLint("NewApi")
Then I marked with // this 3 lines once I unmarked and used the line int t = thumbnail.getByteCount(); so after I took a photo it crashed and told me it need to force close.
Why does it crash on this line ?
This is the file activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="45dp"
android:layout_marginTop="62dp"
android:text="Activate The Camera" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
I can't figure out why it crash with the int t line.
I added a breakpoint on the int t line but it never stop there and never stop anywhere on my application when I add a breakpoint why ?

The getByteCount() is available from API level 12 (android 3.1+).
Are you sure you are building/running this version or above?
Also please add LogCat in the feature, much easier to find the error.

Related

Click ImageButton, take a picture and save photo in same imagebutton, even if leave application Android

I want to build an application that when I click the ImageButton to take a picture that image becomes the default Imagebutton image forever until I change again.
I can take a picture and it becomes the ImageButton image. The problem right now is that when I leave the application the image disappears from Imagebutton.
Help me find a solution for that code, or showing me another code. Thanks
MainActivity
package com.example.camera;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton btnTackPic;
Uri photoPath;
static int TAKE_PICTURE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTackPic = (ImageButton) findViewById(R.id.imageButton1);
btnTackPic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PICTURE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap)intent.getExtras().get("data");
btnTackPic.setImageBitmap(photo);
btnTackPic.setVisibility(View.VISIBLE);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.camera.MainActivity" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
You have several options to store your app data on the device. For images, the best and fastest solution if the to use an image caching technique. If you're looking for a fast solution, check the default caching implementation provided by the devs. Once you have your bitmap ready, store it to your cache. Later on when the app reloads, first thing you do is to check your cache for any stored bitmaps, and load it into your imageButton.

button hovering not working in android

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- <TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="24dp"
android:text="#string/name" /> -->
<EditText
android:id="#+id/etname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ems="10" android:inputType="text"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etname"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="Login" />
</RelativeLayout>
I have a button, I want when I "hover" over the button it shows a hello message with a toast.
I have a button in the layout. I tried to fetch it by findViewById in the FirstActivity. Then I tried using button.setOnHoverListener but it isn't working.
package com.example.datapass;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
final EditText name ;
Button loginButton ;
//final Context context = this;
/** Called when the activity is first created. */
//name= (EditText) findViewById(R.id.etname) ;
final TextView tv = (TextView) (findViewById(R.id.textView1) );
loginButton = (Button) findViewById(R.id.button1);
loginButton.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("hover", "Bring yor cursor over the button");
if(event.getAction()==MotionEvent.ACTION_HOVER_ENTER)
{
//tv.setText("hi");
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Can anyone explain what's wrong?
It would be worth changing your onHover method to be like below and break once it carries out an individual action:
#Override
public boolean onHover(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
I would also advise changing this line as this is not standard format/syntax:
final TextView tv = (TextView) (findViewById(R.id.textView1) );
To be this instead:
final TextView tv = (TextView) findViewById(R.id.textView1);
However I think your actual issue is more relating to the support on mobile devices of hovering as none of your code actually seems like it would break this code section. The majority of devices can only recognise hovers whilst using a stylus and not actually just with a finger. Only some of the higher end devices can actually recognise a hover action with just a finger. This might be something to note and may be advisable to use a swipe or a click instead of a hover to display the Toast.

android statement do not execute after setText in TextView

i am developing a small application in android. my activity_main consists of three things Button ,TextView and ImageView
and here is the code of main activity
package com.example.shiv.facedetection;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onthetapbuttonclick(View View){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
try{
Bitmap bp = (Bitmap) data.getExtras().get("data");
ImageView iv = (ImageView)findViewById(R.id.iv1);
Bitmap bitmap565 = bp.copy(Bitmap.Config.RGB_565, true);
if((1==(bitmap565.getWidth()%2))){
bitmap565 = Bitmap.createScaledBitmap(bitmap565,
bitmap565.getWidth()+1, bitmap565.getHeight(), false);
}
FaceDetector fd = new FaceDetector(bitmap565.getWidth(),bitmap565.getHeight(), 4);
Face[] faces =new Face[4];
int i =fd.findFaces(bitmap565, faces);
TextView tv =(TextView)findViewById(R.id.text1);
if(i>0){
iv.setImageBitmap(bp);
System.out.println("face ////////////////detectecd");
tv.setText("faces detected "+i);
}
else{
iv.setImageBitmap(bp);
System.out.println("no face ////////////////detectecd");
tv.setText("No face detected");
System.out.print("hello world");
}
}catch(Exception e ){
System.out.println("exception occured");
}
}
}
and activity_main is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="click to take the photo"
android:id="#+id/button"
android:onClick="onthetapbuttonclick"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="status"/>
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/iv1"
android:contentDescription="taken photo"
/>
</LinearLayout>
now problem is when i click the image every thing work pretty fine.
if a face is detected then it will show the face detected + number of faces in the TextView and set the clicked pic in the ImageView and will print no face detected if face is not detected, but after that no statement is getting executed.
e.g in "no face ////////////////detected" is getting printed but "hello world " is not getting printed.
I have tried updating the TextView using a Thread. but problem persists
I have tested it many times and both android studio for linux as well as for windows. Kindly help

comparing Text from Speech To Text API to text in a TextView

Brief description of Application thus far:
The user sees a TextView that has a word. The user will click on an ImageButton that will trigger the Speech To Text API and say the word given on the TextView. If the text from the Speech To Text and text in TextView match, then the TextView will change text to, "correct!" etc.
The issue is comparing the string text values. The TextView has "hello." The SpeechToText api returns "Hello" when I say "Hello," but the text values are considered different.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Main"
android:orientation="vertical" >
<ImageButton
android:id="#+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#android:drawable/ic_btn_speak_now" />
<TextView
android:id="#+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"/>
<TextView
android:id="#+id/givenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:layout_gravity="center" />
</LinearLayout>
Main.Java
package com.example.speechtotext;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView inputText;
private TextView givenText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText = (TextView)findViewById(R.id.inputText);
givenText = (TextView)findViewById(R.id.givenText);
btnSpeak = (ImageButton)findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try{
startActivityForResult(intent, RESULT_SPEECH);
inputText.setText("");
}catch(ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"your device does not support Speech to Text!",Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
inputText.setText(text.get(0));
if(givenText.getText().toString().equals(inputText.getText().toString())){
givenText.setText("Correct!");
}
else{
givenText.setText("Incorrect!");
}
}
break;
}
}
}
}
Any Ideas?
The problem you are facing can be solved easily by changing one line in your code, i.e:
if(givenText.getText().toString().equals(inputText.getText().toString())){
Change that to:
if(givenText.getText().toString().equalsIgnoreCase(inputText.getText().toString())){
Also, a general observation that I made when I was working on a similar app, I noticed that the Speech Recognition API isn't perfect and will not work well unless you/your user has an accent that matches the one that the API was trained on. So, in order to fix this you should consider going through the ArrayList of strings returned and checking ALL of them to see if any one of them matches the text you showed the user.

Android: Simpify many classes and xml

Edit: I cannot get this to work correctly. Probably because I have no idea what I am doing. Anyways, here's my code. If anyone could help, I'd be very grateful: I needs to get it to display the battery mood image for the corresponding mod...
Themes:
package com.cydeon.plasmamodz;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Themes extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
ActionBar actionBar = getActionBar();
actionBar.hide();
Button Plus = (Button) findViewById(R.id.button1);
Button Blue = (Button) findViewById(R.id.button2);
Plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Themes.this, Bmod.class);
i.putExtra("drawableResource", R.drawable.blue);
Themes.this.startActivity(i);
}
});
Blue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent a = new Intent(Themes.this, Bmod.class);
a.putExtra("drawableResource1", R.drawable.plus);
Themes.this.startActivity(a);
}
});
}
}
Bmods:
package com.cydeon.plasmamodz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class Bmod extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.blue);
ImageView img = (ImageView) findViewById(R.id.iv1);
img.setImageResource(R.drawable.blue);
Intent a = getIntent();
int drawableResource1 = a.getIntExtra("drawableResource1", R.drawable.plus);
ImageView img1 = (ImageView) findViewById(R.id.iv1);
img1.setImageResource(R.drawable.plus);
}
}
battery(xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bInstall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Install" />
<Button
android:id="#+id/bReturn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Return" />
<ImageView
android:id="#+id/iv1"
android:layout_width="match_parent"
android:layout_height="800dp" />
</RelativeLayout>
Intent i = new Intent(this, BaseClassForMod);
i.putExtra("drawableResource", R.drawable.this_mod_drawable);
startActivity(i);
Then in that Activity's onCreate():
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.default);
//Get a reference to your ImageView...
imageView.setImageResource(drawableResource);
Don't trust this code to compile, but that's the general idea. Use the same Activity for all of them, pass along the proper resource ID in the intent (e.g. for mod1, send the drawable ID for mod1), then in the activity, check for that resource ID and set it programmatically.
You shouldn't need 50 classes and 50 xml layouts if every one of them does the same thing. Make one activity and one layout. When the user selects something, pass an id of some kind as an Intent extra to the second activity so it can load whatever item is appropriate. I don't know how your data is modeled, but there should be a way to uniquely identify each option (and if there isn't, you should implement one).
Your first activity also doesn't need a button for each item. Use a ListView and an Adapter, and then you just need to provide a layout for one row.
make a single activity only. Inside of it get a reference to your image:
ImageView iv = (ImageView)findViewById(R.id.yourImgId);
Then set the picture to whichever one you want like:
iv.setImageResource(R.drawable.battery_img_1);

Categories

Resources