2013-03-23 65 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