Question

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:

enter image description here

please help me out in this as soon as possible. Its urgent.

Thank You

Was it helpful?

Solution 2

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();
  1. 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);

  2. 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());
    }
}
}

OTHER TIPS

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
 startActivityForResult(intent, REQUEST_CAMERA);

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
    if (requestCode == REQUEST_CAMERA)
    {
        File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles())
            {
                if (temp.getName().equals("temp.jpg"))
                {
                    f = temp;
                    break;
                }
            }
            try 
            {
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
                bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions);
                bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
                ivRecipeImage.setImageBitmap(bm);

                path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Phoenix" + File.separator + "default";
                f.delete();
                OutputStream fOut = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try 
                {
                    fOut = new FileOutputStream(file);
                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                }
                catch (FileNotFoundException e) 
                {
                    e.printStackTrace();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                } 
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            } 
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        else if (requestCode == PICK_IMAGE) 
        {
            Uri selectedImageUri = data.getData();
            String tempPath = getPath(selectedImageUri, RecipeActivity.this);
            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
            ivRecipeImage.setImageBitmap(bm);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top