1

我试图在单击按钮后打开相机,然后在屏幕上显示捕获的图片,但是当我单击按钮时没有任何反应。相机没有打开。下面是我的 java 程序的代码及其对应的 xml 文件.

我的课:

package com.example.splashscreen;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class CameraappActivity extends Activity implements View.OnClickListener{
    ImageButton imb1;
    ImageView imv1;
    Button b1;
    final static int cameradata=1;
    Bitmap bmp;
    Intent i1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cameraapp);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.cameraapp, menu);
        initialise();
        imb1.setOnClickListener(this);
        b1.setOnClickListener(this);
        return true;
    }

    private void initialise() {
        // TODO Auto-generated method stub
        imv1=(ImageView)findViewById(R.id.imview1);
        imb1=(ImageButton)findViewById(R.id.imb2);
        System.out.println("Hello");
        b1=(Button)findViewById(R.id.setw);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
        case R.id.imb2:i1=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        System.out.println("entered");
        startActivityForResult(i1, cameradata);
            break;
        case R.id.setw:
            try{
            getApplicationContext().setWallpaper(bmp);}
            catch(IOException e){
                e.printStackTrace();
            }
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("Hi1");
        if(resultCode==RESULT_OK)
        {
            System.out.println("Hi2");
            Bundle b1=data.getExtras();
            bmp=(Bitmap)b1.get("data");
            imv1.setImageBitmap(bmp);
        }

    }


   }

我的 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=".CameraappActivity" >

    <ImageButton
        android:id="@+id/imb2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@+id/setw"
        android:layout_alignLeft="@+id/setw"
        android:layout_marginBottom="24dp"
        android:src="@drawable/sunset1" 
        android:contentDescription="Take picture"/>

    <ImageView
        android:id="@+id/imview1"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_above="@+id/imb2"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="96dp"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/setw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:text="@string/sew" />

</RelativeLayout>
4

2 回答 2

0

试试这对我来说就像魅力一样

private String selectedImagePath = "";
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;

public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }


    public String getImagePath() {
        return imgPath;
    }

btnGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);

            }
        });

        btnCapture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {
                selectedImagePath = getAbsolutePath(data.getData());
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else if (requestCode == CAPTURE_IMAGE) {
                selectedImagePath = getImagePath();
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }


public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
于 2013-09-12T11:56:16.593 回答
0

在 onCreate 方法而不是 OncreateOptionsMenu 中调用您的初始化方法,因为单击侦听器仅应用它们。

于 2013-09-12T11:59:18.123 回答