How to use a clicked picture in a new activity?

后端 未结 2 789
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 23:48

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

相关标签:
2条回答
  • 2020-12-11 00:09

    I already had this problem, Here's my code bug

    Bitmap photo = (Bitmap) data.getData();// This code return null
    

    My problem was resolved by modifying the code

    Bitmap photo = (Bitmap) data.getExtras().get("data"); 
    
    0 讨论(0)
  • 2020-12-11 00:27

    To start the camera activity you can use follow code

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

    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();
       }  
    } 
    
    0 讨论(0)
提交回复
热议问题