I am new in Android programming and I am trying to implement the application which have two buttons to open a camera and make a photo and to open a gallery and select photo.
I know how to do it using the onClick()
in designer mode, but the problem is, that I don't want to have everything in a one big class. So I made two different classes to handle the camera and gallery selection.
And here is my code:
public class Camera extends Activity implements Button.OnClickListener {
private static final int CAMERA_REQUEST = 1888;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap original = (Bitmap) extras.get("data");
ImageView iv = (ImageView) findViewById(R.id.oko);
iv.setImageBitmap(original);
}
}
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
Gallery class:
public class Gallery extends Activity implements Button.OnClickListener {
@Override
public void onActivityResult(int requestCode , int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
if(requestCode ==1) {
ImageView oko = (ImageView)findViewById(R.id.oko);
oko.setImageURI(data.getData()); // pobranie z galerii
Bitmap okobit = ((BitmapDrawable) oko.getDrawable()).getBitmap();
ImageView iv = (ImageView) findViewById(R.id.oko);
iv.setImageBitmap(okobit);
}
}
}
@Override
public void onClick(View view) {
Intent gallery = new Intent();
gallery.setType("image/*");
gallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(gallery, "Select Image"), 1); // here is a NULL exception
}
}
and the method which uses those classes:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button gallery = (Button)findViewById(R.id.gallery);
gallery.setOnClickListener(new Gallery());
Button camera = (Button)findViewById(R.id.camera);
camera.setOnClickListener(new Camera());
}
And now I am wondering why do I have the null pointer exception ? Both, in startActivityForResult connected with Camera and startActivityForResult connected with Gallery
java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3390)
at android.app.Activity.startActivityForResult(Activity.java:3351)
at com.example.teczowka.app.Gallery.onClick(Gallery.java:39)
my xml file:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.teczowka.app.MainActivity"
android:background="#dbff56"
android:name="android.hardware.camera"
android:required="true">
<ImageView
android:layout_width="550dp"
android:layout_height="350dp"
android:id="@+id/oko"
android:visibility="visible" />
<Button
android:layout_width="110dp"
android:layout_height="50dp"
android:text="Recognize"
android:id="@+id/recognize"
android:layout_alignParentBottom="true" />
<Button
android:layout_width="110dp"
android:layout_height="50dp"
android:text="Gallery"
android:id="@+id/gallery"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="110dp"
android:layout_height="50dp"
android:text="Camera"
android:id="@+id/camera"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:longClickable="true"
android:onClick="onClick" />
Your other activities(Gallery & Camera) will need a layout without a layout the activity creation will fail.
I suggest you don't have Gallery and Camera extend Activity Class, but rather you can send the context of the parent activity and use it to start the activity
Update: Here is the solution, Hope it helps
Main Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_camera);
Button gallery = (Button) findViewById(R.id.gallery);
gallery.setOnClickListener(new Gallery(this));
Button camera = (Button) findViewById(R.id.camera);
camera.setOnClickListener(new Camera(this));
}
@Override
public void onActivityResult(int requestCode , int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
if(requestCode ==1) {
ImageView oko = (ImageView)findViewById(R.id.oko);
oko.setImageURI(data.getData()); // pobranie z galerii
Bitmap okobit = ((BitmapDrawable) oko.getDrawable()).getBitmap();
ImageView iv = (ImageView) findViewById(R.id.oko);
iv.setImageBitmap(okobit);
}
}
}
Gallery Class
public class Gallery implements View.OnClickListener {
private Activity mContext;
public Gallery(Activity activity){
this.mContext = activity;
}
@Override
public void onClick(View view) {
Intent gallery = new Intent();
gallery.setType("image/*");
gallery.setAction(Intent.ACTION_GET_CONTENT);
mContext.startActivityForResult(Intent.createChooser(gallery, "Select Image"), 1); // here is a NULL exception
}
}
Camera Class
public class Camera implements View.OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private Activity mContext;
public Camera(Activity activity) {
this.mContext = activity;
}
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mContext.startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.