1

我正在开发一个在图像中产生效果的 android 应用程序。我希望我的应用程序的用户有两个选择。

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的初学者!:(

4

1 回答 1

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);
        }  
    } 
}
于 2014-11-18T07:20:12.810 回答