2

我正在尝试开发一个动态创建布局的应用程序。在我的布局中,我正在创建一个UploadImage按钮。

我正在OnClickListener 另一个 java 类中创建implements`OnClickListener' 类,以便可以在我的应用程序的不同位置使用它。

但是,不幸的是,我无法启动相机活动。

下面是我的应用程序的源代码。

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>

生成视图.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);
}
}

从这个GenerateViewsjava 类中,我正在调用UploadImage实际实现 OnClickListener 的类。

上传图片.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);
    }
}
}

一切正常。我的表单是动态创建的,所有的文本等都按照我的意愿显示。甚至OnClickListener是在调用UploadImage.java类。但这是我得到的错误:

在此处输入图像描述

请尽快帮我解决这个问题。这很紧急。

谢谢你

4

2 回答 2

2
 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);
        }
    }
}
于 2013-08-08T06:29:25.117 回答
0

你可以在这里查看这个项目。我很久没有更新它了;)但它会满足你的需要

脚步

1.在android中创建一个新项目,右键单击它并选择Android,现在单击底部的add,将“Library”项目添加到您的项目中

2.将这些权限放在您的清单文件中

<uses-features android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/> 

以及应用程序标签下的此活动

<activity android:name="com.camera.library.CameraLibrary"></activity>

3.现在获取类的实例

 CameraOptions options = CameraOptions.getInstance(this);
 options.takePicture();
  1. 在按钮的单击事件或任何活动调用中用于启动相机

    Intent intent = new Intent(this,CameraLibrary.class); startActivityForResult(意图,REQUEST_CODE);

  2. 用户拍照时调用options.getBitmapFile()方法获取位图图像或使用options.getFilePath()方法获取文件路径

创建的图像存储在应用程序的缓存目录中

编辑

像这样更改您的代码

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());
    }
}
}
于 2013-08-08T09:58:05.290 回答