2013-03-23 64 views
1

我的工作產生的圖像效果的Android應用程序。我希望我的應用的用戶有兩個選項。相機未returing回我捕捉到的圖像

1)他們可以從圖庫中選擇圖片

2)他們可以從相機拍攝新照片

這裏是我如何完成上述兩項任務:

Button takePhotoFromCameraButton; 
Button chooseFromGalleryButton; 


String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + System.currentTimeMillis() + "_image.jpg"; 
File imageFile = new File(imagePath); 
imageUri = Uri.fromFile(imageFile); 

在兩個按鈕,我傳遞相同的onClickListner。

@Override 
public void onClick(View clickedView) { 

    int clickedViewId = clickedView.getId(); 

    switch(clickedViewId) { 
     case R.id.takeFromCamera: 
      Intent imageCaptureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      imageCaptureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri); 
      startActivityForResult(imageCaptureIntent,0); 
      break; 
     case R.id.chooseFromGallery: 
      Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(choosePictureIntent, 1); 
      break; 
     default: 
      // As we have only two buttons, and nothing else can be clicked except the buttons. So no need to put 
      // code in the "DEFAULT CASE" 
    } 
} 

而且我從兩個捕獲的結果,通過以下方式:

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 

    switch(requestCode) { 
     case 0: 
      Intent cameraIntent = new Intent(MainOptionsActivity.this,ApplyEffectsActivity.class); 
      cameraIntent.putExtra("imageFileUri", imageUri); 
      startActivity(cameraIntent); 
      break; 
     case 1: 
      Uri imageUriForGallery = intent.getData(); 
      Intent galleryIntent = new Intent(MainOptionsActivity.this,ApplyEffectsActivity.class); 
      galleryIntent.putExtra("imageFileUri", imageUriForGallery); 
      startActivity(galleryIntent); 
      break; 
    } 

} 

}

的按鈕畫廊圖像正常工作。但是當我通過按下第二個按鈕調用攝像頭並捕捉圖像時,什麼都不會發生。它停留在那裏直到我取消相機並回到我的應用程序。我沒有收到任何圖像! 我哪裏錯了?不要介意這個愚蠢的問題,我只是一個Android的初學者! :(

+0

沒有幫助......我用一個onActivityResult方法爲兩種觀點,而你寫的鏈接,不這樣做。 – 2013-03-23 17:01:33

+0

會發生什麼? onActivityResult是否永遠不會調用,或者調用了錯誤的值? – Jacopofar 2013-03-23 17:32:43

+0

你爲什麼要在onActivityResult中開始活動..?如果你想接收圖像,你需要做的位圖照片=(位圖)data.getExtras()。get(「data」);像這樣.. – Pragnani 2013-03-23 17:37:56

回答

1

當我實現了我的,我做了這樣的工作,通過一個片段:

public class ConcertFragment extends Fragment implements SurfaceHolder.Callback { 

ImageView toto; 
ToggleButton btnFlashlight; 
RayMenu menu; 
View rootView; 

private Camera cam; 
boolean hasCamera; 
Parameters params; 
private int REQUEST_IMAGE_CAPTURE; 
Bitmap photo; 
Uri imageUri; 

public ConcertFragment() { 
} 

public void startCameraIntent() { 
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE); 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    SurfaceView preview = (SurfaceView)getView().findViewById(R.id.background); 
    SurfaceHolder mHolder = preview.getHolder(); 
    mHolder.addCallback(this); 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.concert, menu); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getCamera(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    rootView = inflater.inflate(R.layout.fragment_concert, container, false); 
    toto = (ImageView) rootView.findViewById(R.id.toto); 
    return rootView; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(resultCode != 0) 
    { 
     if (requestCode == REQUEST_IMAGE_CAPTURE) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      toto.setImageBitmap(photo); 

      View content = rootView.findViewById(R.id.toto); 
      content.setDrawingCacheEnabled(true); 

       Bitmap bitmap = content.getDrawingCache(); 
       File root = Environment.getExternalStorageDirectory(); 
       File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"); 
       try { 
        cachePath.createNewFile(); 
        FileOutputStream ostream = new FileOutputStream(cachePath); 
        bitmap.compress(CompressFormat.JPEG, 100, ostream); 
        ostream.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       Intent share = new Intent(Intent.ACTION_SEND); 
       share.setType("image/*"); 
       share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath)); 
       startActivity(Intent.createChooser(share,"Share via")); 
     } 
     else { 
      cam.release(); 
     } 
    } 
}  

// Get the camera 
    private void getCamera() { 
     if (cam != null) { 
      try { 
       cam = Camera.open(); 
       params = cam.getParameters(); 
       cam.startPreview(); 
       hasCamera = true; 

      } catch (RuntimeException e) { 
       Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); 
       cam.release(); 
      } 
     } 
    } 

希望這有助於:)如果你需要更多的信息,只要問。

如何與活動做到這一點,而不是片段

public class MyCameraActivity extends Activity { 
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     this.imageView = (ImageView)this.findViewById(R.id.imageView1); 
     Button photoButton = (Button) this.findViewById(R.id.button1); 
     photoButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cameraIntent, CAMERA_REQUEST); 
      } 
     }); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      imageView.setImageBitmap(photo); 
     } 
    } 
} 
+0

不能使用片段。必須通過Activiy來完成。 – 2014-11-18 10:45:18