-1

如果已经有人问过这个问题,我深表歉意,但我的代码似乎在某个时候卡住了,只是无限期地试图在我的平板电脑(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 /

这是我认为清单中需要的所有内容的片段:

<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。

4

2 回答 2

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

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

于 2013-03-07T22:04:01.667 回答
0

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

于 2013-03-06T19:40:20.563 回答