无限加载图像屏幕

时间:2013-03-06 18:46:49

标签: java android

如果已经提出这个问题,我很抱歉,但是我的代码有问题似乎在某些时候陷入困境,只是无限期地尝试在我的平板电脑(Nexus 7)裁剪程序上加载图像。我的程序应该允许用户拍照然后裁剪。从那里我将添加更多,但尚未添加该部分。也许我的问题是我的清单中缺少一条线?在我去裁剪图像后,Logcat弹出一行“V 03-06 13:43:52.296 7349 7349 StateManager destroy”。我正在使用Eclipse IDE并使用此站点帮助我完成程序的这一部分:http://mobile.tutsplus.com/tutorials/android/capture-and-crop-an-image-with-the-device-camera/

以下是我认为Manifest中需要的所有内容的片段:

<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" />

这是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"
tools:context=".Scan" >

<Button
    android:id="@+id/capture_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/capture" />

<ImageView
    android:id="@+id/picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/capture_btn"
    android:layout_centerHorizontal="true"
    android:background="@drawable/pic_border"
    android:contentDescription="@string/picture" />

</RelativeLayout>

最后,这是.java文件:

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class Scan extends Activity implements OnClickListener {

//keep track of camera capture intent
final int CAMERA_CAPTURE = 1;
//captured picture uri
private Uri picUri;
//keep track of cropping intent
final int PIC_CROP = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan);
    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);

    //retrieve a reference to the UI button
    Button captureBtn = (Button)findViewById(R.id.capture_btn);
    //handle button clicks
    captureBtn.setOnClickListener(this);


}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.capture_btn) {
        try {
            //use standard intent to capture an image
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //we will handle the returned data in onActivityResult
            startActivityForResult(captureIntent, CAMERA_CAPTURE);
        }
        catch(ActivityNotFoundException anfe){
            //display an error message
            String errorMessage = "Whoops! Your device doesn't support image capturing!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

@Override
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){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        }
        //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 void performCrop(){
    try {
        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate output X and Y. Edit this line for width and height respectively
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);
    }
    catch(ActivityNotFoundException anfe){
        //display an error message
        String errorMessage = "Whoops! Your device doesn't support the cropping!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

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

2 个答案:

答案 0 :(得分:0)

检查您传递给作物活动的picUri的值。如果我将null传递给手机上的裁剪活动,我也会获得无限加载。如果picUri为null,请尝试按照此http://developer.android.com/training/camera/photobasics.html进行拍照。在那里,他们将输出文件的路径传递给捕获活动。

答案 1 :(得分:0)

与我的父亲交谈后,他是一位电气工程师,他已经编程了多年并且看过他的一些程序;我对onActivityResult做了一些更改,并添加了一个解决问题的保存方法。

新的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;
}

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

相关问题