0

目前我正在尝试做一个小应用程序,我需要在其中拍照并在 ImageView 中使用这张照片来显示它。

我使用意图功能来做到这一点。我可以把照片存储在卡上,但我无法在我想要的图像视图中显示它。

我总是得到一个空点异常

这是我使用的代码

private void takePhoto() {

    Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
    String fileName = "testphoto.jpg";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,
            "Image capture by camera");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    outputFileUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, PHOTO_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_REQUEST_CODE) {
        if (resultCode == RESULT_OK){
            Log.d("ANDRO_CAMERA","Picture taken!!!");

            Bitmap yourSelectedImage = BitmapFactory.decodeFile(outputFileUri.toString());
            int h = 48; // height in pixels
            int w = 48; // width in pixels    
            Bitmap scaled = Bitmap.createScaledBitmap(yourSelectedImage, h, w, true);
            imageView.setImageBitmap(scaled);
        }
    }
}

这是我的错误代码

    06-11 01:10:52.812: W/dalvikvm(6469): threadid=1: thread exiting with uncaught exception (group=0x40c8a300)
06-11 01:10:52.832: E/AndroidRuntime(6469): FATAL EXCEPTION: main
06-11 01:10:52.832: E/AndroidRuntime(6469): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.uni.schnitzeljagd/com.uni.schnitzeljagd.NewQuestion}: java.lang.NullPointerException
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.app.ActivityThread.access$1100(ActivityThread.java:130)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.os.Looper.loop(Looper.java:137)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at java.lang.reflect.Method.invoke(Method.java:511)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at dalvik.system.NativeStart.main(Native Method)
06-11 01:10:52.832: E/AndroidRuntime(6469): Caused by: java.lang.NullPointerException
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:461)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at com.uni.schnitzeljagd.NewQuestion.onActivityResult(NewQuestion.java:94)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.app.Activity.dispatchActivityResult(Activity.java:5192)
06-11 01:10:52.832: E/AndroidRuntime(6469):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
06-11 01:10:52.832: E/AndroidRuntime(6469):     ... 11 more

如果有人可以帮助我,那就太好了

最好的祝愿

4

1 回答 1

0

这些链接可以帮助您完成任务:

http://www.javatpoint.com/android-camera-tutorial

http://www.androidhive.info/2013/09/android-working-with-camera-api/

这是捕获照片并显示它的完整类:

     package com.tag.photocaptureandgallery;

     import java.io.File;
     import java.io.FileNotFoundException;
     import java.io.FileOutputStream;
     import java.io.IOException;
     import java.io.OutputStream;

     import android.app.Activity;
     import android.app.AlertDialog;
     import android.content.DialogInterface;
     import android.content.Intent;
     import android.database.Cursor;
     import android.graphics.Bitmap;
     import android.graphics.BitmapFactory;
     import android.net.Uri;
     import android.os.Bundle;
     import android.os.Environment; 

     import android.provider.MediaStore;
     import android.provider.MediaStore.MediaColumns;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.Button;
     import android.widget.ImageView;

    public class MainActivity extends Activity {

private final int SELECT_FILE = 1;
private final int REQUEST_CAMERA = 0;
private ImageView ivImage;
private Button btnSetImage;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ivImage = (ImageView) findViewById(R.id.ivImage);
    btnSetImage = (Button) findViewById(R.id.btnSelectPhoto);

    btnSetImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            selectImage();
        }
    });

}

private void selectImage() {
    final CharSequence[] items = { "Take Photo", "Choose from Library",
            "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                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);
            } else if (items[item].equals("Choose from Library")) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

@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 {
                Bitmap bm;
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();

                bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        btmapOptions);

                // bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
                ivImage.setImageBitmap(bm);

                String 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 == SELECT_FILE) {
            Uri selectedImageUri = data.getData();

            String tempPath = getPath(selectedImageUri, MainActivity.this);
            Bitmap bm;
            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
            ivImage.setImageBitmap(bm);
        }
    }
}

public String getPath(Uri uri, Activity activity) {
    String[] projection = { MediaColumns.DATA };
    Cursor cursor = activity
            .managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}

希望这会帮助你。

于 2013-11-18T09:24:47.870 回答