How to access an image from the phone's photo gallery? - android

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" />

Related

Trouble in Implementing Camera Images Storage in SQLite Database on Android

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>

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.

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

Android SQLite : Take and Save Photo to DB as Base64 string... Then retrieve

I would like to develop a full-proof solution to a common problem for new android developers. I want to create a tutorial and example (for myself and others to follow) to taking a photo witin an app and then saving it to base64 string within a SQLite android database. Then I will be able to upload this string to an online database at a later stage, rather than the JPG image I originally took.
So to re-iterate it must...
Open Camera,
Take Image
Save as Base64 String to SQLite DB
(the reason that I need it to be stored in the DB as a string, is due to the fact that this will be uploaded to a central document system at some point in the future, and large JPG images will be slow and not ideal, hence why a Base64 string in the DB would be better. I also want to limit the possibility that users can delete the images they take which would be referenced in the DB should be just refrence the path to the file (which is the alternative - although I believe a less suitable one))
Thanks, I really look forward to working on this and developing a great solution that others will be able to follow.
MY EFFORTS SO FAR...
I have spent a number of days looking into this, and at the moment I am more confused than at the start. There seems to be so much outthere about this, but nothing with step by step sections to follow, about what line does what, where it comes from, what its doing etc. So to start with, I think I may need to break this down into two tasks now...
Taking an image using the camera and giving the file as an output somehow...then saving this as a base64 string to the DB or something like this.
I appreciate that usually storing binary data to the DB can cause slow and painful queries, however due to the face that only a few images will ever be displayed at once, we shouldn't have too much of an issue here, and the SQLite queries are small.
So to start with I was following Android: how to take picture with camera and convert bitmap to byte array and save to sqlite db? however the instructions to save images locally and reference the file path, really don't work. and so I was back to square one almost...
I have been reading through http://developer.android.com/guide/topics/media/camera.html to learn about the camera... confused.com !!!
To be fair, I have read so much, and now understand so little, I need a dummies guide to this now. Wish I could unlearn all the useless crap which I have read about this and start from scratch... Where is Format /F for your brain?
OK so i started again...
Things are going a lot better for me now, I have even added audio, but I won't get into that. My app at the moment, takes a photo, previews it (using the standard camera intent) and then on clicking save, displays it locally on the app. Instead of it displaying the image, on clicking save, I need it to save to a database as a blob/bit64
Here is my code so you can see where I am at...
trying to use this as the call to take the image...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inspection ID" />
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/txtName"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1" />
<EditText
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:gravity="top|left"
android:lines="4"
android:maxLines="4"
android:minLines="4"
android:scrollbars="vertical"
android:singleLine="false"
android:width="0dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Ref"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/txtAge"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drop Down"
/>
<Spinner
android:id="#+id/spinDept"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnPhotoCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="camera" />
<Button
android:id="#+id/btnPhotoGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="gallery" />
<TextView
android:id="#+id/lblDisplayImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCancel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="below_this_text_image_will_be_displayed"
android:textSize="13dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/lblDisplayImage"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:gravity="bottom" >
<!--
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
-->
<ImageView
android:id="#+id/imgDisplayImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="area_where_image_is_to_be_displayed" />
<!-- </ScrollView> -->
</RelativeLayout>
<Button
android:id="#+id/btnAudio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnAudio"
android:text="Audio" />
<Button
android:id="#+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnAddEmp_Click"
android:text="Save Inspection" />
<Button
android:id="#+id/btnCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="#+id/btnPhotoGallery"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Reset/Clear Form Data" />
<TextView
android:id="#+id/txtEmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of Inspections on Device " />
</LinearLayout>
</ScrollView>
</LinearLayout>
with the following .java
package mina.android.DatabaseDemo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Spannable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import com.AssentApp.V100.R;
public class AddEmployee extends Activity {
EditText txtName;
EditText txtAge;
TextView txtEmps;
DatabaseHelper dbHelper;
Spinner spinDept;
/** The Constant PICK_IMAGE. */
private static final int PICK_IMAGE = 0;
/** The Constant PICK_IMAGE_FROM_GALLERY. */
private static final int PICK_IMAGE_FROM_GALLERY = 1;
/** The btn cancel. */
private Button btnPhotoCamera,btnPhotoGallery,btnCancel;
/** The img view. */
private ImageView imgView;
/** The u. */
private Uri u;
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addemployee);
txtName=(EditText)findViewById(R.id.txtName);
txtAge=(EditText)findViewById(R.id.txtAge);
txtEmps=(TextView)findViewById(R.id.txtEmps);
spinDept=(Spinner)findViewById(R.id.spinDept);
imgView=(ImageView)findViewById(R.id.imgDisplayImage);
btnPhotoCamera=(Button)findViewById(R.id.btnPhotoCamera);
btnPhotoGallery=(Button)findViewById(R.id.btnPhotoGallery);
btnCancel=(Button)findViewById(R.id.btnCancel);
btnPhotoCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent camera=new Intent();
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra("crop", "false");
File f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
u = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myFile.jpg"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, u);
startActivityForResult(camera, PICK_IMAGE);
}
});
btnPhotoGallery.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE_FROM_GALLERY);
}
});
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent goStartUp=new Intent(AddEmployee.this, AddEmployee.class);
goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goStartUp);
finish();
}
});
}
/* (non-Javadoc)
* #see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode==RESULT_OK )
{
if(requestCode == PICK_IMAGE) {
InputStream is=null;
try {
is = this.getContentResolver().openInputStream(u);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp=BitmapFactory.decodeStream(is);
imgView.setImageBitmap(bmp);
Log.i("Inside", "PICK_IMAGE");
}
if (requestCode == PICK_IMAGE_FROM_GALLERY) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Log.d("data",filePathColumn[0]);
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imgView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Log.i("Inside", "PICK_IMAGE_FROM_GALLERY");
}
}
}
public void onStart()
{
try
{
super.onStart();
dbHelper=new DatabaseHelper(this);
txtEmps.setText(txtEmps.getText()+String.valueOf(dbHelper.getEmployeeCount()));
Cursor c=dbHelper.getAllDepts();
startManagingCursor(c);
//SimpleCursorAdapter ca=new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, c, new String [] {DatabaseHelper.colDeptName}, new int []{android.R.id.text1});
SimpleCursorAdapter ca=new SimpleCursorAdapter(this,R.layout.deptspinnerrow, c, new String [] {DatabaseHelper.colDeptName,"_id"}, new int []{R.id.txtDeptName});
//ca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinDept.setAdapter(ca);
spinDept.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View selectedView,
int position, long id) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//never close cursor
}
catch(Exception ex)
{
CatchError(ex.toString());
}
}
public void btnAddEmp_Click(View view)
{
boolean ok=true;
try
{
Spannable spn=txtAge.getText();
String name=txtName.getText().toString();
int age=Integer.valueOf(spn.toString());
int deptID=Integer.valueOf((int)spinDept.getSelectedItemId());
Employee emp=new Employee(name,age,deptID);
dbHelper.AddEmployee(emp);
}
catch(Exception ex)
{
ok=false;
CatchError(ex.toString());
}
finally
{
if(ok)
{
//NotifyEmpAdded();
Alerts.ShowEmpAddedAlert(this);
txtEmps.setText("Number of Inspections on Device "+String.valueOf(dbHelper.getEmployeeCount()));
}
}
}
void CatchError(String Exception)
{
Dialog diag=new Dialog(this);
diag.setTitle("Adding new Inspection");
TextView txt=new TextView(this);
txt.setText(Exception);
diag.setContentView(txt);
diag.show();
}
void NotifyEmpAdded()
{
Dialog diag=new Dialog(this);
diag.setTitle("Success");
TextView txt=new TextView(this);
txt.setText("Inspection Added Successfully");
diag.setContentView(txt);
diag.show();
try {
diag.wait(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
CatchError(e.toString());
}
diag.notify();
diag.dismiss();
}
public void btnAudio(View view)
{
Intent intent = new Intent(AddEmployee.this, AudioRecordTest.class);
startActivity(intent);
}
}
seems to do the trick, but now I need to edit it for the DB section. I need to save it "by converting image into Byte[] then save as Blob in sqlite" - or so I believe ... Whats my first step in doing this? Note, I don't want it to display the image view anymore, but instead save to DB with unique ID/Integer.
any pointers would be great !!
i don't think u can do it using base64 , but yes you can do that thing by converting image into Byte[] then save as Blob in sqlite.

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

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.

Categories

Resources