Android 当我在不同设备中尝试时,应用程序内的摄像头强制关闭数据为空

Android 当我在不同设备中尝试时,应用程序内的摄像头强制关闭数据为空,android,android-intent,nullpointerexception,android-camera,android-sdcard,Android,Android Intent,Nullpointerexception,Android Camera,Android Sdcard,我有一个apk应用程序,它可以用一个按钮拍照和录像 这是我的活动 package com.example.testcamera; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import

我有一个apk应用程序,它可以用一个按钮拍照和录像

这是我的活动

package com.example.testcamera;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;

public class AndroidCameraTestsActivity extends Activity 
{
    private static final String TAG = AndroidCameraTestsActivity.class.getSimpleName(); 

    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;

    private Uri fileUri;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    /** 
     * https://developer.android.com/guide/topics/media/camera.html 
     * **/
    public void onCaptureImage(View v) 
    {
        // give the image a name so we can store it in the phone's default location
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "IMG_" + timeStamp + ".jpg");

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image (this doesn't work at all for images)
        fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); // store content values
        intent.putExtra( MediaStore.EXTRA_OUTPUT,  fileUri);

        // start the image capture Intent
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }

    /** 
     * https://developer.android.com/guide/topics/media/camera.html 
     * **/
    public void onCaptureVideo(View v) 
    {
         //create new Intent
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        //fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video in specific folder (this works for video only)
        //intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name

        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

        // start the Video Capture Intent
        startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                // Originally I was going to iterate through the list of images and grab last added to the MediaStore.
                // But this is not necessary if we store the Uri in the image
                /*
                String[] projection = {MediaStore.Images.ImageColumns._ID};
                String sort = MediaStore.Images.ImageColumns._ID + " DESC";

                Cursor cursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, sort);

                try{
                    cursor.moveToFirst();
                    Long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID));
                    fileUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
                } finally{
                    cursor.close();
                }
                */

                if(fileUri != null) {
                    Log.d(TAG, "Image saved to:\n" + fileUri);
                    Log.d(TAG, "Image path:\n" + fileUri.getPath());
                    Log.d(TAG, "Image name:\n" + getName(fileUri)); // use uri.getLastPathSegment() if store in folder
                }

            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
            } else {
                // Image capture failed, advise user
            }
        }

        if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                // Video captured and saved to fileUri specified in the Intent
                fileUri = (Uri) data.getData();

                if(fileUri != null) {
                    Log.d(TAG, "Video saved to:\n" + fileUri);
                    Log.d(TAG, "Video path:\n" + fileUri.getPath());
                    Log.d(TAG, "Video name:\n" + getName(fileUri)); // use uri.getLastPathSegment() if store in folder
                }

            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the video capture
            } else {
                // Video capture failed, advise user
            }
        }
    }

    /** Create a file Uri for saving an image or video to specific folder
     * https://developer.android.com/guide/topics/media/camera.html#saving-media
     * */
    private static Uri getOutputMediaFileUri(int type)
    {
          return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type)
    {
        // To be safe, you should check that the SDCard is mounted

        if(Environment.getExternalStorageState() != null) {
            // this works for Android 2.2 and above
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AndroidCameraTestsFolder");

            // This location works best if you want the created images to be shared
            // between applications and persist after your app has been uninstalled.

            // Create the storage directory if it does not exist
            if (! mediaStorageDir.exists()) {
                if (! mediaStorageDir.mkdirs()) {
                    Log.d(TAG, "failed to create directory");
                    return null;
                }
            }

            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile;
            if (type == MEDIA_TYPE_IMAGE){
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
            } else if(type == MEDIA_TYPE_VIDEO) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_"+ timeStamp + ".mp4");
            } else {
                return null;
            }

            return mediaFile;
        }

        return null;
    }

    // grab the name of the media from the Uri
    protected String getName(Uri uri) 
    {
        String filename = null;

        try {
            String[] projection = { MediaStore.Images.Media.DISPLAY_NAME };
            Cursor cursor = managedQuery(uri, projection, null, null, null);

            if(cursor != null && cursor.moveToFirst()){
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
                filename = cursor.getString(column_index);
            } else {
                filename = null;
            }
        } catch (Exception e) {
            Log.e(TAG, "Error getting file name: " + e.getMessage());
        }

        return filename;
    }
}
package com.exercise.AndroidCamera;

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

import android.app.Activity;
import android.content.ContentValues;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;

    final int RESULT_SAVEIMAGE = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

    }
        public void onCaptureImage(View v) 
        {

        setContentView(R.layout.main);

        Toast.makeText(AndroidCamera.this, 
                "Image dimulai : ", 
                Toast.LENGTH_LONG).show();
        getWindow().setFormat(PixelFormat.UNKNOWN);

        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.control, null);
        LayoutParams layoutParamsControl 
            = new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

        Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
        buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                camera.takePicture(myShutterCallback, 
                        myPictureCallback_RAW, myPictureCallback_JPG);

            }});
    }


    ShutterCallback myShutterCallback = new ShutterCallback(){

        @Override
        public void onShutter() {
            // TODO Auto-generated method stub

        }};

    PictureCallback myPictureCallback_RAW = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub

        }};

    PictureCallback myPictureCallback_JPG = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub
            /*Bitmap bitmapPicture 
                = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */

            Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());

            OutputStream imageFileOS;
            try {
                imageFileOS = getContentResolver().openOutputStream(uriTarget);
                imageFileOS.write(arg0);
                imageFileOS.flush();
                imageFileOS.close();

                Toast.makeText(AndroidCamera.this, 
                        "Image Tersimpan: " + uriTarget.toString(), 
                        Toast.LENGTH_LONG).show();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            /*controlInflater = LayoutInflater.from(getBaseContext());
            View viewControl = controlInflater.inflate(R.layout.confirm, null);
            LayoutParams layoutParamsControl 
                = new LayoutParams(LayoutParams.FILL_PARENT, 
                LayoutParams.FILL_PARENT);
            this.addContentView(viewControl, layoutParamsControl);*/

        }};

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {

                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }



    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        camera = Camera.open();
        //m_camera = Camera.open();
        /*Camera.Parameters p = camera.getParameters();

        //camera.setDisplayOrientation(90);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {   
            p.set("orientation", "portrait");
            p.set("rotation",90);
        }
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {                               
            p.set("orientation", "landscape");          
            p.set("rotation", 90);
        }*/


    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
}
我有第一台设备运行应用程序,我有两个按钮,
拍照
拍摄视频

当我在此应用程序中单击
拍摄视频
时,它工作正常,但当我从按钮中单击
拍摄照片
时,应用程序总是“强制关闭”

这是我的错误日志

11-19 14:43:27.085: ERROR/AndroidRuntime(6903): FATAL EXCEPTION: main
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): java.lang.NullPointerException
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at com.android.camera.Camera.initializeFirstTime(Came ra.java:328)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at com.android.camera.Camera.access$1100(Camera.java: 95)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at com.android.camera.Camera$MainHandler.handleMessag e(Camera.java:282)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at android.os.Handler.dispatchMessage(Handler.java:99 )
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at android.os.Looper.loop(Looper.java:130)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at android.app.ActivityThread.main(ActivityThread.jav a:3683)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at java.lang.reflect.Method.invoke(Method.java:507)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:839)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:597)
11-19 14:43:27.085: ERROR/AndroidRuntime(6903): at dalvik.system.NativeStart.main(Native Method)
11-19 14:43:27.093: WARN/ActivityManager(1308): Force finishing activity com.android.camera/.Camera
11-19 14:43:27.109: WARN/ActivityManager(1308): Force finishing activity makemachine.android.examples/.PhotoCaptureExample

编辑:这是我的一个应用程序按钮不同的活动

package com.example.maincamera;

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

public class OpenCameraDemo extends Activity {

    private static final int CAMERA_PIC_REQUEST = 2500;

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

        Button b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                 startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
              Bitmap image = (Bitmap) data.getExtras().get("data");
              ImageView imageview = (ImageView) findViewById(R.id.ImageView01);
              imageview.setImageBitmap(image);
        }
    }
}
这个应用程序仍然错误太多,我已经尝试了许多应用程序。在google中搜索。错误仍然与我的logcat错误相同

当我尝试在其他设备上运行它时,这个应用程序工作得非常好

如何修复此问题,以便我可以在第一台设备中运行并拍照

比尔


Alex

崩溃发生在设备的摄像头应用程序内部,而不是您自己的应用程序。不幸的是,由于此设备上的摄像头应用程序可能是AOSP中实际存在的大量定制变体,因此不完全清楚故障是什么,而是在发生异常的AOSP 2.3.7源代码树中。行号与任何感兴趣的东西都不完全匹配,这告诉我您的特定设备上的应用程序至少已经定制了一些

initializeFirstTime()
方法中发生异常、调用方法的任何对象都可能是问题的根源,但这些都与您的请求传入的
Intent
参数无关,因此,要使其正常运行,您能做的工作可能不多


因此,您自己在自己的应用程序中实现捕获功能的运气可能会更好。这台设备上的Android API可能比它捆绑的系统应用程序更稳定。您可以如何创建自己的相机预览和拍摄活动。

我对您的手机很好奇。他们有什么不同

由于您的onCaptureImage(视图v)确实描述了外部存储。 但是,onCaptureVideo(视图v)没有

  • 电话坏了。是否支持外部sd卡?如果不是,则所有代码都是关于ext.sd的

  • 是你的手机坏了吗?偶尔把sd卡拿出来?尝试将其连接为摄像头或mtp

  • 朋友做一件事

    制作只有一个按钮的简单应用程序
    onClick
    事件照片应为捕获-不添加其他任务


    它工作正常吗?

    尝试捕获异常并在此处发布消息:

    try{
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    }
    catch(ActivityNotFoundException e){
    e.printStackTrace();
    }
    

    谢谢Devunwired,谢谢chintan,谢谢大家对我的帮助。我使用摄像头api,它可以正常工作

    这是我的活动

    package com.example.testcamera;
    
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.View;
    
    public class AndroidCameraTestsActivity extends Activity 
    {
        private static final String TAG = AndroidCameraTestsActivity.class.getSimpleName(); 
    
        private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
        private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
        public static final int MEDIA_TYPE_IMAGE = 1;
        public static final int MEDIA_TYPE_VIDEO = 2;
    
        private Uri fileUri;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        /** 
         * https://developer.android.com/guide/topics/media/camera.html 
         * **/
        public void onCaptureImage(View v) 
        {
            // give the image a name so we can store it in the phone's default location
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, "IMG_" + timeStamp + ".jpg");
    
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
            //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image (this doesn't work at all for images)
            fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); // store content values
            intent.putExtra( MediaStore.EXTRA_OUTPUT,  fileUri);
    
            // start the image capture Intent
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    
        /** 
         * https://developer.android.com/guide/topics/media/camera.html 
         * **/
        public void onCaptureVideo(View v) 
        {
             //create new Intent
            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    
            //fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video in specific folder (this works for video only)
            //intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name
    
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
    
            // start the Video Capture Intent
            startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
    
                    // Originally I was going to iterate through the list of images and grab last added to the MediaStore.
                    // But this is not necessary if we store the Uri in the image
                    /*
                    String[] projection = {MediaStore.Images.ImageColumns._ID};
                    String sort = MediaStore.Images.ImageColumns._ID + " DESC";
    
                    Cursor cursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, sort);
    
                    try{
                        cursor.moveToFirst();
                        Long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID));
                        fileUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
                    } finally{
                        cursor.close();
                    }
                    */
    
                    if(fileUri != null) {
                        Log.d(TAG, "Image saved to:\n" + fileUri);
                        Log.d(TAG, "Image path:\n" + fileUri.getPath());
                        Log.d(TAG, "Image name:\n" + getName(fileUri)); // use uri.getLastPathSegment() if store in folder
                    }
    
                } else if (resultCode == RESULT_CANCELED) {
                    // User cancelled the image capture
                } else {
                    // Image capture failed, advise user
                }
            }
    
            if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
    
                    // Video captured and saved to fileUri specified in the Intent
                    fileUri = (Uri) data.getData();
    
                    if(fileUri != null) {
                        Log.d(TAG, "Video saved to:\n" + fileUri);
                        Log.d(TAG, "Video path:\n" + fileUri.getPath());
                        Log.d(TAG, "Video name:\n" + getName(fileUri)); // use uri.getLastPathSegment() if store in folder
                    }
    
                } else if (resultCode == RESULT_CANCELED) {
                    // User cancelled the video capture
                } else {
                    // Video capture failed, advise user
                }
            }
        }
    
        /** Create a file Uri for saving an image or video to specific folder
         * https://developer.android.com/guide/topics/media/camera.html#saving-media
         * */
        private static Uri getOutputMediaFileUri(int type)
        {
              return Uri.fromFile(getOutputMediaFile(type));
        }
    
        /** Create a File for saving an image or video */
        private static File getOutputMediaFile(int type)
        {
            // To be safe, you should check that the SDCard is mounted
    
            if(Environment.getExternalStorageState() != null) {
                // this works for Android 2.2 and above
                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AndroidCameraTestsFolder");
    
                // This location works best if you want the created images to be shared
                // between applications and persist after your app has been uninstalled.
    
                // Create the storage directory if it does not exist
                if (! mediaStorageDir.exists()) {
                    if (! mediaStorageDir.mkdirs()) {
                        Log.d(TAG, "failed to create directory");
                        return null;
                    }
                }
    
                // Create a media file name
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                File mediaFile;
                if (type == MEDIA_TYPE_IMAGE){
                    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
                } else if(type == MEDIA_TYPE_VIDEO) {
                    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
                } else {
                    return null;
                }
    
                return mediaFile;
            }
    
            return null;
        }
    
        // grab the name of the media from the Uri
        protected String getName(Uri uri) 
        {
            String filename = null;
    
            try {
                String[] projection = { MediaStore.Images.Media.DISPLAY_NAME };
                Cursor cursor = managedQuery(uri, projection, null, null, null);
    
                if(cursor != null && cursor.moveToFirst()){
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
                    filename = cursor.getString(column_index);
                } else {
                    filename = null;
                }
            } catch (Exception e) {
                Log.e(TAG, "Error getting file name: " + e.getMessage());
            }
    
            return filename;
        }
    }
    
    package com.exercise.AndroidCamera;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.OutputStream;
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.res.Configuration;
    import android.graphics.PixelFormat;
    import android.hardware.Camera;
    import android.hardware.Camera.PictureCallback;
    import android.hardware.Camera.ShutterCallback;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore.Images.Media;
    import android.util.Log;
    import android.view.Display;
    import android.view.LayoutInflater;
    import android.view.Surface;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
    
        Camera camera;
        SurfaceView surfaceView;
        SurfaceHolder surfaceHolder;
        boolean previewing = false;
        LayoutInflater controlInflater = null;
    
        final int RESULT_SAVEIMAGE = 0;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home);
    
        }
            public void onCaptureImage(View v) 
            {
    
            setContentView(R.layout.main);
    
            Toast.makeText(AndroidCamera.this, 
                    "Image dimulai : ", 
                    Toast.LENGTH_LONG).show();
            getWindow().setFormat(PixelFormat.UNKNOWN);
    
            surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
            surfaceHolder = surfaceView.getHolder();
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
            controlInflater = LayoutInflater.from(getBaseContext());
            View viewControl = controlInflater.inflate(R.layout.control, null);
            LayoutParams layoutParamsControl 
                = new LayoutParams(LayoutParams.FILL_PARENT, 
                LayoutParams.FILL_PARENT);
            this.addContentView(viewControl, layoutParamsControl);
    
            Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
            buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    camera.takePicture(myShutterCallback, 
                            myPictureCallback_RAW, myPictureCallback_JPG);
    
                }});
        }
    
    
        ShutterCallback myShutterCallback = new ShutterCallback(){
    
            @Override
            public void onShutter() {
                // TODO Auto-generated method stub
    
            }};
    
        PictureCallback myPictureCallback_RAW = new PictureCallback(){
    
            @Override
            public void onPictureTaken(byte[] arg0, Camera arg1) {
                // TODO Auto-generated method stub
    
            }};
    
        PictureCallback myPictureCallback_JPG = new PictureCallback(){
    
            @Override
            public void onPictureTaken(byte[] arg0, Camera arg1) {
                // TODO Auto-generated method stub
                /*Bitmap bitmapPicture 
                    = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */
    
                Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
    
                OutputStream imageFileOS;
                try {
                    imageFileOS = getContentResolver().openOutputStream(uriTarget);
                    imageFileOS.write(arg0);
                    imageFileOS.flush();
                    imageFileOS.close();
    
                    Toast.makeText(AndroidCamera.this, 
                            "Image Tersimpan: " + uriTarget.toString(), 
                            Toast.LENGTH_LONG).show();
    
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
    
                /*controlInflater = LayoutInflater.from(getBaseContext());
                View viewControl = controlInflater.inflate(R.layout.confirm, null);
                LayoutParams layoutParamsControl 
                    = new LayoutParams(LayoutParams.FILL_PARENT, 
                    LayoutParams.FILL_PARENT);
                this.addContentView(viewControl, layoutParamsControl);*/
    
            }};
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub
            if(previewing){
                camera.stopPreview();
                previewing = false;
            }
    
            if (camera != null){
                try {
    
                    camera.setPreviewDisplay(surfaceHolder);
                    camera.startPreview();
                    previewing = true;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
    
            camera = Camera.open();
            //m_camera = Camera.open();
            /*Camera.Parameters p = camera.getParameters();
    
            //camera.setDisplayOrientation(90);
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            {   
                p.set("orientation", "portrait");
                p.set("rotation",90);
            }
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {                               
                p.set("orientation", "landscape");          
                p.set("rotation", 90);
            }*/
    
    
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            camera.stopPreview();
            camera.release();
            camera = null;
            previewing = false;
        }
    }
    
    我创建home.xml来显示按钮图像,就像我的问题一样

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Button android:text="Capture Image" android:onClick="onCaptureImage" 
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    </LinearLayout>
    
    
    
    这是一个相机预览

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <SurfaceView
        android:id="@+id/camerapreview"  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />
    </LinearLayout>
    
    
    
    并在相机预览中创建control.xml“拍照”按钮

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom"
        >
    <Button
        android:id="@+id/takepicture"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text=" * Ambil Foto " 
        android:layout_gravity="right"
        android:layout_margin="10px"
        />
    
    
    </LinearLayout> 
    
    
    
    但我在这个应用程序中有两个问题

  • 相机分辨率不是全尺寸-+20kb
  • “拍摄摄影机预览”处于“旋转90度”状态时

  • 这两款设备有什么不同?第一款是来自中国的MT35A,另一款是axioo vigo。当我使用axioo时,应用程序工作,但当我移动到MT35A时,捕获视频工作,但拍照是强制关闭的。您是否有其他android手机,以便我们可以解决主要问题。?我认为该硬件可能不支持它。最低api版本是什么?axioo 2.3.4和MT35A 2.3.7。刚才我试着将这个应用程序安装到LG ics 4.0.3上,然后拍摄相机工作。但我很好奇,三星仍然不起作用。好的,我将编辑我的问题,并用一个按钮添加活动。谢谢@德文维德。我试着使用摄像头API,它可以正常工作。但我有两个问题。1.当我拍摄相机时,相机无法获得完整的图像分辨率。照相机旋转90度。谢谢你的回答。我能问你一件事吗。这是关于这个肋骨照相机的。如何将自定义摄像头与onActivityResult连接?请查看Android开发者活动结果培训。任何活动都可以将结果传递给其父级,并且您的应用程序可以使结果数据看起来与相机应用程序中的结果数据相同: