Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';当前置摄像头工作时,我读取手机主摄像头产生的文件_Java_Android_Camera - Fatal编程技术网

Java 为什么可以';当前置摄像头工作时,我读取手机主摄像头产生的文件

Java 为什么可以';当前置摄像头工作时,我读取手机主摄像头产生的文件,java,android,camera,Java,Android,Camera,我正在尝试加载LG G8手机内置摄像头拍摄的照片。 该代码适用于前置摄像头,但若我将其切换到后端,则会引发空指针异常 static final int DESIRED_WIDTH = 640; static final int DESIRED_HIGH = 480; private Bitmap retrieveBitmap(){ // Get the dimensions of the bitmap BitmapFactory.Options bitmapOptions =

我正在尝试加载LG G8手机内置摄像头拍摄的照片。 该代码适用于前置摄像头,但若我将其切换到后端,则会引发空指针异常

static final int DESIRED_WIDTH = 640;
static final int DESIRED_HIGH = 480;

private Bitmap retrieveBitmap(){
    // Get the dimensions of the bitmap
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    //decode only size
    bitmapOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(currentPhotoPath, bitmapOptions);

    //returns 0 x 0
    int photoW = bitmapOptions.outWidth;
    int photoH = bitmapOptions.outHeight;

    // Determine how much to scale down the image
    float scaleFactor = Math.min( (float) photoW/ (float) DESIRED_WIDTH,
            (float)  photoH/ (float) DESIRED_HIGH);

    // Decode the image file into a Bitmap of given size
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    bitmapOptions.inJustDecodeBounds = false;
    bitmapOptions.inSampleSize = (int) scaleFactor;

    //returns null
    Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bitmapOptions);
    return  bitmap;
}
使用“保存全尺寸照片”方法调用Camera应用程序。Android在第一次调用BitmapFactory.decodeFile()后报告NullPointerException,就好像主摄像头生成的文件不存在一样

E/BitmapFactory:无法解码流:java.lang.NullPointerException


不久前,我也使用了你提到的指南,可以使用手机摄像头拍照并保存它们

下面的代码通过点击按钮激活手机摄像头,并允许前后摄像头拍照,然后进行保存。它还显示在ImageView中拍摄的照片。希望能有帮助

public class MainActivity extends AppCompatActivity {

    static final int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        galleryAddPic();
        ImageView img = findViewById(R.id.img);
        Bitmap bitm = BitmapFactory.decodeFile(mCurrentPhotoPath);
        img.setImageBitmap(bitm);
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.fileprovider", photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    String mCurrentPhotoPath;

    private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName, ".jpg", storageDir);

        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }


    public void cameraClick(View v){
        dispatchTakePictureIntent();
    }
}
回答我自己的问题: 事实证明,手机需要一段时间才能访问大图片。添加等待循环可使其工作:

    private Bitmap retrieveBitmap(){
    // Get the dimensions of the bitmap
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    //decode only size
    bitmapOptions.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(currentPhotoPath, bitmapOptions);
    int i = 0;
    while( bitmapOptions.outWidth == 0 && bitmapOptions.outHeight == 0){
        //wait for 4 seconds for resource to be available, otherwise fail
        try{
            wait(1000);
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        BitmapFactory.decodeFile(currentPhotoPath, bitmapOptions);
        i++;
        //give up trying
        if( i == 4) break;
    }

    //returns 0 x 0
    int photoW = bitmapOptions.outWidth;
    int photoH = bitmapOptions.outHeight;

    // Determine how much to scale down the image
    float scaleFactor = Math.min( (float) photoW/ (float) DESIRED_WIDTH,
            (float)  photoH/ (float) DESIRED_HIGH);

    // Decode the image file into a Bitmap of given size
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    bitmapOptions.inJustDecodeBounds = false;
    bitmapOptions.inSampleSize = (int) scaleFactor;

    return  BitmapFactory.decodeFile(currentPhotoPath, bitmapOptions);
}