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
Related
I have recently made an App which has a basic Intent which opens the default camera app on device.
But I want to expand it's functionality which will enable it to store the captured image from camera to the SQLite Database. Along with displaying the images already stored in the database. Also I want to display the Date & Time of the image which is captured below the image, as well as Add a Comment box below the date/time of the image.
The code for the MainActivity is as follows:
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCamera = (Button) findViewById(R.id.btnCamera);
imageView = (ImageView) findViewById(R.id.imageView);
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
}
Code for the Activity's Layout XML is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:orientation="vertical"
android:weightSum="10"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="#+id/btnCamera"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_dark"
android:textColor="#android:color/white"
android:text="Open Camera"
/>
</LinearLayout>
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.
currently i made a 'speechtotext' app using the google speech to text api.
It takes whatever i speak near the mic as an input and displays the text form of it by sending it to google and getting the result from it.
Well,instead of speaking,i want to give an audio file as the input(which i have previously recorded using 'sound recorder' app) to my "speech to text" app.
This audio file is present in the sdcard of the phone.
Google speech to text api should choose this audio file as the input and then give its text form as the output.
I have pasted my "100% working" code of the xml and the java files for your references.
my xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#+id/textView1"
android:layout_toLeftOf="#+id/textView1"
android:background="#drawable/superim"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/tap_on_the_button_to_speak_"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<ImageButton
android:id="#+id/btnSpeak"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/speak"
android:src="#android:drawable/ic_btn_speak_now" />
<TextView
android:id="#+id/txtText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="0.38"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
/>
</LinearLayout>
my java file:
package com.prann.speechtotextdemo;
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 MainActivity extends Activity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtText = (TextView) findViewById(R.id.txtText);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"YOUR DEVICE DOESNT SUPPORT SPEECH TO TEXT \n install google vocie search ",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#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);
txtText.setText(text.get(0));
}
break;
}
}
}
}
Is it possible to do what i want to do ?? if so,how can it be done ?????
A detailed explanation would be very appreciated.
Thank you in advance. :-)
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.
By any chance, does anyone know how to access the phone's photo gallery?
I am making an application that takes a picture of a plant leaf and
analyzes the image to determine whether or not it is determine. We were hoping that we
could give the user two options of taking the picture of the leaf or using an image of a
leaf that the user has already taken. However, we got the picture taking part, but we do not
know how to access the photo gallery.
You have to launch the Gallery App using the built-in Intents. After that, on your onActivityResult(), get the path of the selected image and load your image into your ImageView
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button
android:id="#+id/loadimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Image"
/>
<TextView
android:id="#+id/targeturi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/targetimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Your Activity
package com.exercise.AndroidSelectImage;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class AndroidSelectImage extends Activity {
TextView textTargetUri;
ImageView targetImage;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
textTargetUri = (TextView)findViewById(R.id.targeturi);
targetImage = (ImageView)findViewById(R.id.targetimage);
buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Do not forget to add the following permissions to AndroidManifest.xml:
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />