I am trying to develop an app where the layout is created dynamically.
In my layout, I am creating anUploadImage
button.
I am creating the OnClickListener
in another java class which implements
the `OnClickListener' class, so that can use it at different places in my app.
But, Unfortunately, I am unable to start the camera activity.
Below is the source code of my application.
###MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
GenerateViews genView = new GenerateViews();
genView.addQuestionWithUploadImageOption(this, layout);
}
###activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
###GenerateViews.java
public class GenerateViews {
void addQuestionWithUploadImageOption(final Context context,
LinearLayout layout) {
// TODO Auto-generated method stub
LinearLayout layoutImage = new LinearLayout(context);
layoutImage.setOrientation(LinearLayout.HORIZONTAL);
layoutImage.setWeightSum(1);
layoutImage.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ImageView imageView = new ImageView(context);
imageView.setContentDescription("Image");
imageView.setLayoutParams(new LayoutParams(256, 256));
Button uploadImageButton = new Button(context);
uploadImageButton.setText(R.string.uploadImage);
uploadImageButton.setOnClickListener(new UploadImage());
layoutImage.addView(imageView);
layoutImage.addView(uploadImageButton);
layout.addView(layoutImage);
}
}
From this GenerateViews
java class, I am calling UploadImage
class which is actually implementing the OnClickListener.
###UploadImage.java
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class UploadImage extends Activity implements OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Everything is working fine. My form is being created dynamically and all the texts and etc are displayed as I want.
Even the OnClickListener
is calling the UploadImage.java
class.
But this the error which I am getting:
What can I try next?
You can check this project here . I havent updated it for quite long time ;) but it will suit your need
STEPS
1.Create a new project in android , right click on it and Choose Android and now click add at the bottom to add the "Library" project to your project
2.Place these permission in your Manifest file
<uses-features android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>
and also this activity under the application tag
<activity android:name="com.camera.library.CameraLibrary"></activity>
3.Now get the instance of the class
CameraOptions options = CameraOptions.getInstance(this);
options.takePicture();
on the click event of button or in any activity call use to start the camera
Intent intent = new Intent(this,CameraLibrary.class); startActivityForResult(intent, REQUEST_CODE);
Call the options.getBitmapFile()
method to get the bitmap image when user takes the picture or use the options.getFilePath()
method to get the file path
The images created are stored in the cache directory of the application
EDIT
Change your code like this
public class UploadImage extends Activity implements OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private CameraOptions options;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
options = CameraOptions.getInstance(UploadImage.this);
options.takePicture();
options.setRequesCode(CAMERA_REQUEST);
Intent intent = new Intent(UploadImage.this,CameraLibrary.class);
startActivityForResult(intent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
imageView.setImageBitmap(options.getBitmapFile());
}
}
}