2013-03-06 54 views
-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被返回爲空。

+0

備選解決方案非常感謝。 – Kami 2013-03-06 21:44:47

回答

0

檢查您傳遞給作物活動的picUri的值。如果我將null傳遞給手機上的剪裁活動,我也會得到無限的加載。如果picUri爲空,請嘗試按照此http://developer.android.com/training/camera/photobasics.html拍攝照片。在那裏他們將輸出文件的路徑傳遞給捕獲活動。

+0

你說他們將輸出文件傳遞給捕獲活動?我無法理解或遵循開發者網站上的一半亂碼。 – Kami 2013-03-06 19:55:47

+0

'File f = createImageFile(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(f));'但在執行此操作之前,最好檢查'performCrop()'中的picUri值。如果它不爲空,則沒有理由改變拍攝照片的方式。 – 2013-03-06 19:59:55

+0

該部分討論了在永久保存文件後命名文件。我不想永久保存文件。我想在暫時讓其他程序「讀取」任何文本之前暫時獲取它。之後,文件應該被廢棄。雖然如此,我只是想讓拍照和裁剪部分工作。此外,我可能會想讓用戶決定裁剪圖像的尺寸。我認爲刪除方面的部分和outputx/y可能會做到這一點。 – Kami 2013-03-06 20:17:40

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

現在的問題是,在未來的一部分,作物的方法,這是一個不同的問題都在一起。