Java 无限加载图像屏幕

Java 无限加载图像屏幕,java,android,Java,Android,如果已经有人问过这个问题,我很抱歉,但是我的代码似乎在某个时候卡住了,只是无限期地试图在我的平板电脑(Nexus 7)的裁剪程序中加载图像。我的程序应该允许用户拍摄照片,然后进行裁剪。从那里我会添加更多,但这部分还没有添加。也许我的问题是舱单上少了一行?在我裁剪图像后,Logcat弹出一行“V 03-06 13:43:52.296 7349 7349 StateManager destroy”。我正在使用Eclipse IDE,并使用此网站帮助将此部分纳入我的程序: 以下是我认为清单中需要的全部

如果已经有人问过这个问题,我很抱歉,但是我的代码似乎在某个时候卡住了,只是无限期地试图在我的平板电脑(Nexus 7)的裁剪程序中加载图像。我的程序应该允许用户拍摄照片,然后进行裁剪。从那里我会添加更多,但这部分还没有添加。也许我的问题是舱单上少了一行?在我裁剪图像后,Logcat弹出一行“V 03-06 13:43:52.296 7349 7349 StateManager destroy”。我正在使用Eclipse IDE,并使用此网站帮助将此部分纳入我的程序:

以下是我认为清单中需要的全部内容的一个片段:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />

注意,问题是picUri返回为null。

检查传递给crop活动的picUri值。如果我在手机上将null传递给crop活动,我也会得到无限的加载。如果picUri为null,请尝试按照此操作拍照。在那里,他们将输出文件的路径传递给捕获活动。

与我父亲交谈后,我父亲是一位多年编程的电气工程师,看到了他的一些程序;我对onActivityResult做了一些更改,并添加了一个save方法来解决这个问题

新的onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        //user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){
            //save the pic
            try {
                File f = createImageFile();
                //get the Uri for the captured image
                picUri = Uri.fromFile(f);
                //carry out the crop operation
                performCrop();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //user is returning from cropping the image
        else if(requestCode == PIC_CROP){
            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            //retrieve a reference to the ImageView
            ImageView picView = (ImageView)findViewById(R.id.picture);
            //display the returned cropped image
            picView.setImageBitmap(thePic);
        }
    }
}
新的保存方法:

private File createImageFile() throws IOException {
    String filename = new String("part");
    File image = File.createTempFile(filename, ".jpeg");
    return image;
}

现在问题在下一部分,裁剪方法,这是一个完全不同的问题。

您说他们在哪里将输出文件传递给捕获活动?我无法理解或理解开发者网站上一半的胡言乱语;takePictureContent.putExtra(MediaStore.EXTRA_输出,Uri.fromFile(f))但在执行此操作之前,最好检查
performCrop()
中的picUri值。如果不为空,则没有理由更改拍照方式。这部分内容是指在永久保存文件后命名文件。我不想永久保存该文件。我只想在让另一个程序“读取”任何文本之前暂时获取它。在那之后,文件应该被废弃。再次虽然,我只是想得到拍照和作物的一部分工作。此外,我可能希望让用户决定裁剪图像的尺寸。我认为删除aspect和outputx/y的部分可能会做到这一点。如果选中,则picUri为空。当捕获活动拍摄照片时,它会将其保存在某个位置。在代码中,您尝试在
onActivityResult()
picUri=data.getData()中接收此路径。然后将此URI发送到crop活动。在本文中,他们创建了一个文件路径,将保存拍摄的图像(您可以指定路径,而不使用默认路径)。之后,您将没有空URI。如果需要,您可以在用户剪切图像后删除此文件。非常感谢其他解决方案。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        //user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){
            //save the pic
            try {
                File f = createImageFile();
                //get the Uri for the captured image
                picUri = Uri.fromFile(f);
                //carry out the crop operation
                performCrop();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //user is returning from cropping the image
        else if(requestCode == PIC_CROP){
            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            //retrieve a reference to the ImageView
            ImageView picView = (ImageView)findViewById(R.id.picture);
            //display the returned cropped image
            picView.setImageBitmap(thePic);
        }
    }
}
private File createImageFile() throws IOException {
    String filename = new String("part");
    File image = File.createTempFile(filename, ".jpeg");
    return image;
}