Android: Display Captured image in img tag

后端 未结 1 1007
慢半拍i
慢半拍i 2021-01-16 22:44

I want to display captured image in img tag in html page. I can get the URI value by data.getData() from the OnActivityResult event. But I dont know inwhich

相关标签:
1条回答
  • 2021-01-16 23:18

    Got a Solution. :-)

    public class Camera extends Activity 
    {
         private static final int CAMERA_REQUEST = 1888;
         private String selectedImagePath;
         WebView webview;
         String fileName = "capturedImage.jpg";
         FileOutputStream fo;
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
                webview=(WebView)findViewById(R.id.webView1);
        }
    
        public void TakePhoto()
        {   
    
                Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }       
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if (resultCode == RESULT_OK)
                {
                  if (requestCode == CAMERA_REQUEST) 
                  { 
                    Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                Random randomGenerator = new Random();randomGenerator.nextInt(100);
                String newimagename=randomGenerator.toString()+".jpg";
                File f = new File(Environment.getExternalStorageDirectory()
                                        + File.separator + newimagename);
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //write the bytes in file
    
                try {
                    fo = new FileOutputStream(f.getAbsoluteFile());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    fo.write(bytes.toByteArray());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                webview.loadUrl("javascript:ReceivePhoto(\""+f.getAbsolutePath()+"\")");
                  }
                }
        }
    
        public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
    
    }
    

    ManifestFile:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    0 讨论(0)
提交回复
热议问题