如何使用点击图片在新的活动

如何使用点击图片在新的活动

本文介绍了如何使用点击图片在新的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Android手机的摄像头点击图片,然后使用它的另一项活动。我找不到任何确切的方法,所以我试图让图片的路径在那里保存,然后把它用在其他活动。

I want to use android phone camera to click a picture and then use it in another activity. I could not find any exact method, so I tried to get path of the picture where it is saved and then use it in the other activity.

private OnClickListener cameraBclicked = new OnClickListener() {
    public void onClick(View v) {

        Intent m_Intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(m_Intent, TAKE_PICTURE);
    }
};

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){

    if ( requestCode == TAKE_PICTURE)
    {
        Uri selectedImage = data.getData();
        Intent intent1 = new Intent(picsource.this,NewScreen.class);
        intent1.putExtra("path", selectedImage);
        startActivity(intent1);
    }

现在,问题是出uri出来是空..请纠正上述code ..

Now, the problem is that the uri comes out to be null..please correct the above code..

推荐答案

要开始,你可以使用如下code相机活性

To start the camera activity you can use follow code

Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

captiring形象,你会得到在onActivityResult方法的位图格式拍摄的图像后。现在,当你得到的位图写在外部存储的位图和传递到要通过anothe活动图像的路径。从第二个活动就可以打开该文件,并获得图像。

After captiring image you will get captured image in the bitmap format in onActivityResult method. Now when you get the bitmap write the bitmap in the external storage and the pass the path of the image to anothe activity where you want to pass. From second activity you can open the file and get the image.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == 1) {
        Bitmap bmp = intent.getExtras().get("data");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

         bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
         byte[] byteArray = stream.toByteArray(); // convert camera photo to byte array

         // save it in your external storage.
        FileOutputStream fo = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/_camera.png"));

        fo.write(byteArray);
        fo.flush();
        fo.close();
   }
}

这篇关于如何使用点击图片在新的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 09:46